How do I log at verbose level using `System.Diags...Trace`

Luke Puplett picture Luke Puplett · Mar 3, 2015 · Viewed 9.2k times · Source

Okay don't laugh. In 2005 I read about tracing using System.Diagnostics namespace, it was complicated and I have used log4net and NLog ever since (and so has everyone else).

Today, my app will be hosted on Windows Azure Websites and that uses our old friend, Trace.

http://azure.microsoft.com/en-gb/documentation/articles/web-sites-enable-diagnostic-log/

Smugly, I always used abstractions, IoC, so I'm just writing a new little shim to write using Trace but it only has TraceInformation, TraceWarning and TraceError.

There's some Write* methods but I've not clue where they'll end up and under what circumstances. Horrible API. [gags]

Which method is for verbose/debug level?

Edit: removed "Easy one" from the title. Clearly it is not.

Answer

Jcl picture Jcl · Mar 4, 2015

What you are describing is the System.Diagnostics.Trace class, which does have some simple methods to write to diagnostics trace output.

That's far from how powerful the tracing diagnostics are in .NET

The nicest way to do tracing is to create a TraceSource. In a TraceSource class there's a Switch property which in turns has a Level property where you define which levels of verbosity you want for that specific TraceSource. You can make that tracesource listen to all levels:

var ts = new TraceSource("My Verbose Debugger") {Switch = {Level = SourceLevels.All}};

Then to trace something, you trace to that source, where you specify the level, like this:

ts.TraceData(TraceEventType.Verbose, 0, dataToBeTraced);

The 0 is the id of the trace and dataToBeTraced is an object with the specific data you want to trace (it's a params [] object parameter, so you can pass in many objects if you wish).

Now how to use that data? Using a TraceListener which you add to your TraceSource's Listeners collection. You make your own class deriving from TraceListener and override the TraceData method, like this:

class MyTraceListener : TraceListener
{
  public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
  {
    base.TraceData(eventCache, source, eventType, id, data);
    // do what you want with the objects in the "data" parameter
  }
}

The listener can be shared among many tracesources, and it will only receive the data level that it's TraceSwitch level allows.

System.Diagnostics.Trace uses a listener like this (the DefaultTraceListener) which is added by default to both Debug.Listeners and Trace.Listeners, but internally, it works as I described.

This all might look a bit confusing at first, but it's really powerful, and once you have a set of helper classes... at least I, have stopped using third-party logging libraries and use this to great extent.

As for Azure, this is pure speculation since I've never done any Azure, but I guess you'd configure your tracesource like this in your app.config (maybe web.config? not sure about web), adding a default azure listener for the log:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sharedListeners>
       <add name="AzureListener" type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
           <filter type="" />
       </add>
    </sharedListeners>        
    <sources>
      <source name="MyTraceSource" switchValue="Verbose" >
         <listeners>
            <add name="AzureListener" />
         </listeners>
      </source>
   </sources>
 </system.diagnostics>
</configuration>

"MyTraceSource" is the string name you gave yo your TraceSource in the constructor when doing that via code.

Or you can just create a TraceSource in code like above, and add a Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener to its Listeners collection