Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog time formatting

Tags:

nlog

How do I write a layout for NLog that outputs time with milliseconds like this 11:32:08:123? I use ${date:format=yyyy-MM-dd HH\:mm\:ss} but I need more time precision in my logs.

like image 937
levanovd Avatar asked Sep 20 '10 04:09

levanovd


People also ask

What is NLog config?

What is Nlog ? Nlog is an open source log platform. Through this platform, we can log an activity of an application. This makes an application's code easy to handle.

Which is better NLog or log4net?

There is some small difference between NLog and log4net. NLog is easier to configure, and supports a much cleaner code-based configuration than log4net. I think the defaults in NLog are also more sensible than in log4net.

What is layout in NLog?

Layouts are defining the layout of the rendered output, examples are a CSV, JSON, XML (NLog 4.6+) or plain layout (the default). There are currently 5 layouts in NLog (including the default): https://nlog-project.org/config/?tab=layouts.


2 Answers

${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

like image 141
matthughes404 Avatar answered Sep 18 '22 08:09

matthughes404


${longdate} 

Another alternative to the format suggested by harriyott is to use the ${longdate} renderer. It should automatically give you the precision you need.

like image 40
Ali Avatar answered Sep 19 '22 08:09

Ali