This project has moved and is read-only. For the latest updates, please go here.

Should traceSource.TraceInformation("Information message"); work with the SqlTraceListener?

Jul 21, 2011 at 12:08 PM

I'm just playing with the newly-.Net 2.0-able Essential Diagnostics.  I'm testing out the SqlTraceListener with some existing code.  There was a line of code similar to this:

traceSource.TraceInformation("Information message");

which throws the exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: args
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at Essential.Diagnostics.TraceListenerBase.TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, String format, Object[] args)
   at System.Diagnostics.TraceSource.TraceEvent(TraceEventType eventType, Int32 id, String format, Object[] args)
   at System.Diagnostics.TraceSource.TraceInformation(String message)
   at MyObject...[etc.]

If I change the config to use a text file, the code executes without an exception and produces the same output as this code:

traceSource.TraceData(TraceEventType.Information, 1010, "Information message");

but as if id was = 0.

The workaround is clear but I thought I'd raise it in case something similar might apply elsewhere.

Jul 23, 2011 at 2:17 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 23, 2011 at 2:18 AM

This is probably a bug; I've copied it to a work item to address later. Thanks for posting the work around.

Jul 25, 2011 at 9:03 AM

You're welcome.

And thanks again, for this work.

Jul 25, 2011 at 11:23 AM
Edited Jul 25, 2011 at 12:25 PM

 

In TraceListenerBase.cs, checking for null args in the string.Format call in the TraceEvent() method fixes the exception:


public override sealed void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
	if ((base.Filter == null) || base.Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null))
	{
		string message = string.Format(CultureInfo.CurrentCulture, format, args == null ? new object[0] : args);
		WriteTrace(eventCache, source, eventType, id, message, null, null);
	}
}

 

This makes it work for me.

 

Danny