10

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.

user673979
  • 185
  • 1
  • 5

2 Answers2

19

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.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • 1
    If you don't want to provide formatting for local time then use just: `{timestamp(local)}`. – John K Nov 21 '11 at 21:33
4

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.

Community
  • 1
  • 1
Daniel Dyson
  • 13,192
  • 6
  • 42
  • 73
  • 1
    As I mention, for those looking to log performance tracing of method entries and exits consider the Tracer class which does use Stopwatch. Also see the LogCallHandler which also gives entry & exit timings with an AOP approach. – Randy Levy Sep 20 '12 at 03:27
  • 1
    Indeed, Tracer is a better option for this. What I meant by my answer is that using EntLib for this as per the question is pointless. – Daniel Dyson Sep 20 '12 at 16:13
  • Thank you both for clarification! – gReX Sep 07 '17 at 07:53