NLog time formatting

${date:format=yyyy-MM-dd HH\:mm\:ss.fff} According to the NLog documentation, you can use C# DateTime format string. This is a pretty good reference for DateTime format strings: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

NLog does not create a log file

I had this problem turned out that my log file was not being copied to my build directory. The NLog github page had the answer. (I’ve reformatted the paragraph a little for better readability.) https://github.com/NLog/NLog/wiki/Logging-troubleshooting NLog cannot find the configuration file. This can happen when the NLog.config file is configured with Build Action = None … Read more

How do I log a custom field in NLog to database?

UPDATE 11 Feb 2022: newer versions of NLOG have other solutions- see Julian’s answer. Rather than using GDC, which is for global static data and fails on concurrent logging, it is better to use EventProperties-Layout-Renderer that allows to pass custom  properties specific for the event LogEventInfo theEvent = new LogEventInfo(logLevel, “”, message); theEvent.Properties[“OrderId”] =orderId;` log.Log(theEvent); … and in … Read more

NLog performance

You only need to add the async attribute to your targets element: <targets async=”true”> <target name=”file” xsi:type=”File” fileName=”${basedir}/log.txt” /> instead of <targets> <target name=”asyncFile” xsi:type=”AsyncWrapper”> <target name=”file” xsi:type=”File” fileName=”${basedir}/log.txt” /> </target> I guess I didn’t get that far into the documentation 😉 Asynchronous target wrapper allows the logger code to execute more quickly, by queueing … Read more

How to get path of current target file using NLog in runtime?

This did the trick for me: var fileTarget = (FileTarget) LogManager.Configuration.FindTargetByName(“file”); // Need to set timestamp here if filename uses date. // For example – filename=”${basedir}/logs/${shortdate}/trace.log” var logEventInfo = new LogEventInfo {TimeStamp = DateTime.Now}; string fileName = fileTarget.FileName.Render(logEventInfo); if (!File.Exists(fileName)) throw new Exception(“Log file does not exist.”);

How can I output NLog messages to Visual Studio’s Output Window?

You can use this configuration file (nlog.config in the app path): <?xml version=”1.0″ encoding=”utf-8″ ?> <nlog xmlns=”http://www.nlog-project.org/schemas/NLog.xsd” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”> <targets> <target name=”debugger” xsi:type=”Debugger” layout=”${logger}::${message}”/> </targets> <rules> <logger name=”*” minlevel=”Trace” writeTo=”debugger” /> </rules> </nlog> See also: https://github.com/NLog/NLog/wiki/Debugger-target -Scott

Dependency injection and named loggers

I’m using Ninject to resolve the current class name for the logger instance like this: kernel.Bind<ILogger>().To<NLogLogger>() .WithConstructorArgument(“currentClassName”, x => x.Request.ParentContext.Request.Service.FullName); The constructor of a NLog Implementation could look like this: public NLogLogger(string currentClassName) { _logger = LogManager.GetLogger(currentClassName); } This approach should work with other IOC containers as well, I guess.