48

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.

Julian
  • 33,915
  • 22
  • 119
  • 174
levanovd
  • 4,095
  • 6
  • 37
  • 56
  • 2
    If you happen to be looking for a time only long format, use `${time}`. (How did I not spot this last time I searched? [NLog Layout-Renderers](https://github.com/NLog/NLog/wiki/Layout-Renderers)) – Nigel Touch Oct 21 '15 at 15:21

3 Answers3

100

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

Ed Chapel
  • 6,842
  • 3
  • 30
  • 44
matthughes404
  • 2,427
  • 1
  • 20
  • 9
12
${longdate}

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

Ali
  • 141
  • 1
  • 4
  • 4
    `${longdate}` works great, but be aware that you *must* define the database column as `datetime2` rather than `datetime` or NLog will throw an exception when you try to store the `longdate` value. – Nick Dec 15 '14 at 20:05
11

Another alternative solution is to use the ISO 8601 format '1998-02-23T14:23:05.555'. This format is independet of the languange of the sql server.

${date:format=yyyy-MM-ddTHH\:mm\:ss.fff}
Fux
  • 165
  • 1
  • 7