Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enterprise Library 4.1 logging timestamp how to display millisecond

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.

like image 512
user673979 Avatar asked Jun 01 '11 17:06

user673979


2 Answers

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.

like image 134
Randy supports Monica Avatar answered Oct 06 '22 19:10

Randy supports Monica


Unfortunately, Enterprise library logging uses the .net DateTime so although logging milliseconds is possible as descriped by @Tuzo, it is at best meaningless and at worst misleading.

Eric Lippert wrote an interesting explanation of this: https://stackoverflow.com/a/2143784/230428

The precision of any value will be at best plus or minus 50ms (although this varies form machine to machine - another reason for not relying on this). This is beacuse the date time was designed for answering the question "What Date and Time is it?" rather than the question "How long did that take?". For the latter, use the Stopwatch class.

Obviously, the convenience of having a logging framework to beaver away and record your timings can sound tempting, but you shouldn't rely on it for anything less than second-precision timings.

I first came accross this when my MVC OnActionExecuting and OnActionExecuted logs were returning the very same time to the microsecond. Very impressive and equally wrong.

like image 39
Daniel Dyson Avatar answered Oct 06 '22 19:10

Daniel Dyson