Enterprise Library 4.1 logging timestamp how to display millisecond

user673979 picture user673979 · Jun 1, 2011 · Viewed 9.1k times · Source

The following is in config file.

 <formatters>
      <add template="{timestamp} {severity} {category} {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="SingleLineFormatter" />
 </formatters>

which displays

31/05/2011 11:43:24 Information ...

But there is no millisecond displayed which would be useful for perf instrumentation, anyone knows how to display? Thanks.

Answer

Randy supports Monica picture Randy supports Monica · Jun 1, 2011

You can specify Standard or Custom DateTime Format strings to the timestamp template token:

<formatters>
      <add 
        template="{timestamp(MM/dd/yyyy HH:mm:ss.fffffff)} {severity} {category} {message}" 
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="SingleLineFormatter" />
 </formatters>

This would output something like:

06/01/2011 20:12:43.3405776 Information General This is the message

By default the DateTime will be in UTC time. If you wish to use local time then prefix the format string with "local:". e.g. {timestamp(local:MM/dd/yyyy HH:mm:ss.fffffff)}

Also, if you are looking to log performance tracing of method entries and exits you may want to look at the Tracer class.