Search code examples
c#enterprise-librarylogging-application-block

Get ElapsedTime From Trace


I am trying out the hands on labs for MS Enterprise Library - Logging Application block.

By adding a Trace, I get a 'message' on my 'flat file' listener saying: "Message: End Trace: Activity '6455494c-0602-45c9-8a10-052cdd39e5cb' in method 'EnoughPI.Calc.Calculator.Calculate' at 767616631287998 ticks (elapsed time: 10.292 seconds)"

Can I also get the elapsed time 'alone', or say elapsed tics, from the trace.

Thanks,


Solution

  • The API does not expose the timing information on a public interface. You could use reflection to obtain the information but you do so at your own peril (since it's dependent on the internal implementation which could change):

    using (Tracer tracer = new Tracer("General"))
    {
        FieldInfo fieldInfo = typeof(Tracer).GetField("stopwatch", BindingFlags.NonPublic | BindingFlags.Instance);
        var sw = fieldInfo.GetValue(tracer) as Stopwatch;
        Console.WriteLine(sw.ElapsedMilliseconds);
    }
    

    Another alternative would be to write your own Tracer implementation (based on the Enterprise Library code) which exposes any properties you are interested in.