Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 876
Luke Puplett Avatar asked Mar 03 '15 18:03

Luke Puplett


People also ask

What is verbose logging level?

The Verbose level logs a message for both the activity start and end, plus the values of the variables and arguments that are used. By default, the Verbose level includes: Execution Started log entry - generated every time a process is started. Execution Ended log entry - generated every time a process is finalized.

What is verbose debug logging?

For deeper troubleshooting, a developer can enable verbose logging, and execute the developer version of the same app on an iOS or Android device. Verbose logging records more information than the usual logging mode. Remember to enable it only for troubleshooting, because larger log files can slow down performance.

What is verbose logging in C#?

Verbose logging is a type of computer logging method that involves more information than the standard or typical logging process. Typically, users can turn on verbose logging features to get more information about a system.

How do I check my server's diagnostic trace logs?

Right-click a server. To view the trace log file, select Open Log Files > Trace File from the menu. To view the messages log file, select Open Log Files > Message Log File from the menu.


2 Answers

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

like image 60
Jcl Avatar answered Sep 23 '22 15:09

Jcl


I wanted to add my own story.

The simple answer to my question is that Trace.WriteLine is effectively verbose. However, .NET tracing is powerful but complicated but I managed to get it working into Table Storage.

  • In the Azure portal, turn on Application Logging (Table Storage) under the Configure tab and setup your table. If you make a new one, it won't appear until you save changes.

  • In your application, you only need to use methods on System.Diagnostics.Trace and WAWS will setup the requisite listener automatically.

My stumbling block has been tracing fatal messages appropriately. Since there is no static method for critical level messages, I am forced to use the methods on TraceSource and pass in my desired level enum.

For example, in my logging abstraction, the level comes in as LoggingLevel.Fatal and I need to call TraceSource.TraceEvent(TraceEventType.Critical, ...

However, just newing-up a TraceSource does nothing since, as Jcl explains, it needs a listener. That's where I'm now stuck.

var listener = (TraceListener)new Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener();

_traceSource = new TraceSource(name, SourceLevels.All);
_traceSource.Listeners.Add(listener);

This needed a reference to Microsoft.WindowsAzure.Diagnostics in the SDK locally but it strangely doesn't compile.

That cast there won't work. I shouldn't even need it anyway.

Error 3 Cannot convert type 'Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener' to 'System.Diagnostics.TraceListener' Evoq.AppName.CoreLib C:\DATA\Code\AppName\Evoq.AppName\Evoq.AppName.CoreLib\Instrumentation\AzureApplicationDiagnosticsLogger.cs 30 28

Strange part is that the RedGate Reflector shows an inheritance chain like this:

TraceListener, v4.0_4.0.0.0__b77a5c561934e089\System.dll
RDEventMonitoringAgentListener, MonAgentListener.dll
DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics.dll

If I reflect my TraceSource class there, its (also) from:

v4.0_4.0.0.0__b77a5c561934e089\System.dll

Odd. I'm giving up now. I have a product to ship. I'll revert to using the static Trace class and come back to it if it starts to cost.

Luke

like image 24
Luke Puplett Avatar answered Sep 19 '22 15:09

Luke Puplett