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.

Published 08 March 06 04:31 by kcwalina

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Martin Aupperle said on March 10, 2006 9:23 AM:
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.

Martin

# kcwalina said on March 11, 2006 3:36 AM:
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.
# Alex said on March 20, 2006 12:33 PM:
I would like to extend tracing, specifically TraceEventCache to have more data (for instance, windows identity, host, etc.). How do I do this?
# kcwalina said on March 20, 2006 12:50 PM:
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.

Thanks.
# Josh Einstein said on March 21, 2006 6:33 PM:
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.

http://josheinstein.com/journal/archive/2005/06/08/520.aspx
http://josheinstein.com/journal/archive/2005/06/09/539.aspx
# kcwalina said on March 21, 2006 6:46 PM:
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.
# Max Mulawa said on April 21, 2006 3:53 PM:
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("|");
           strData.Append(data[i].ToString());

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.
# kcwalina said on April 21, 2006 7:44 PM:
Max, you are right. It should be strData.Append(data[i].ToString());
Sorry for that and thanks for catching it.
# Che Clark said on May 13, 2006 6:54 PM:
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."
 Source="System"
 BareMessage="Couldn't find type for class MsdnMagazine.Samples.Tracing.EMailListener ,Extensions."
 Line=0
 StackTrace:
      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:\CLRInsideOut0604\CLRIO\CLRInsideOut0604\Tests\test.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?

Thanks,
Che'
# checlark said on May 13, 2006 7:42 PM:
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?

Thanks,
Che'

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Page view tracker