Extending System.Diagnostics tracing APIs

The April issue of the MSDN Magazine just went on line. It contains an article I wrote that describes some advanced extensibility supported by System.Diagnostic tracing APIs. It talks about creating custom TraceListeners, Switches, and TraceSources.

Comments (10)

  1. Martin Aupperle says:

    What happens when an exception is thrown? The trace code for leaving the function is not executed. In C++, where we have RAII, this is easily solved: put the trace call for leaving a function in the destructor of a class and have a local object of that class in every function.

    What can be done in .NET? Introduce a try…finally block? Hope not.


  2. If you want to ensure trace, try-finally seems to be the right choice. Why not?

    But I would say that if an exception is thrown, you actually don’t want the Exit event. An Enter event, optionally followed by a trace with the excetion, and the absence of the Exit event describe pretty well what actually happened.

  3. Alex says:

    I would like to extend tracing, specifically TraceEventCache to have more data (for instance, windows identity, host, etc.). How do I do this?

  4. We don’t currently support extending the TraceEventCache. But I think it’s a good idea. I would appreciate if you could file it formally at http://lab.msdn.microsoft.com/ProductFeedback/. This way you could track the status of the request.


  5. Check out the custom trace listener I developed. It was mentioned in MSDN magazine before by someone from Wintellect but I thought you might want to give it a look cause it’s very useful.



  6. Josh, this is a very cool idea. A twist on it would be to provide a listener that tracers to an RSS feed. You could subscribe to the feed and see, let’s say, critical events in your blog reader.

  7. Max Mulawa says:

    Hi Krzysztof,

    Really good article, will use EmailListener in my current development work. I’m not sure if in class TraceListener2 (Figure 2) in method

    protected virtual string FormatData(object[] data)

    you should change the If condition to

    if (i != 0) strData.Append("|");


    to append pipe to the  end of each string representation of the data[i] object.

    I don’t know if this is a mistake, but I thought it would be useful if I’m right.

  8. Max, you are right. It should be strData.Append(data[i].ToString());

    Sorry for that and thanks for catching it.

  9. Che Clark says:

    I am trying to run this code but keep getting the following error:

    System.Configuration.ConfigurationErrorsException was unhandled

     Message="Couldn’t find type for class MsdnMagazine.Samples.Tracing.EMailListener ,Extensions."


     BareMessage="Couldn’t find type for class MsdnMagazine.Samples.Tracing.EMailListener ,Extensions."



          at System.Diagnostics.TraceUtils.GetRuntimeObject(String className, Type baseType, String initializeData)

          at System.Diagnostics.TypedElement.BaseGetRuntimeObject()

          at System.Diagnostics.ListenerElement.GetRuntimeObject()

          at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()

          at System.Diagnostics.TraceSource.Initialize()

          at System.Diagnostics.TraceSource.TraceEvent(TraceEventType eventType, Int32 id, String format, Object[] args)

          at System.Diagnostics.TraceSource.TraceInformation(String message)

          at MsdnMagazine.Samples.Tracing.test.Main() in F:CLRInsideOut0604CLRIOCLRInsideOut0604Teststest.cs:line 27

          at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)

          at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

          at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

          at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

          at System.Threading.ThreadHelper.ThreadStart()

    The namespace defined in the config matches exactly the namespace for the EmailListener class.

    Any ideas or direction?



  10. checlark says:

    Sorry if this message is up here twice.

    I am trying to run the code but I keep getting the cannot find type error for the EmailListener. I have checked the namespace and they are identical.

    Any ideas or direction?