When using a Trace.Listener can anyone tell me why the
Trace.Write(string message, string category)
doesn’t pass the category string to the TraceFilter when the method
Trace.Write(object o, string category)
does pass the category string to the ShouldTrace method. Below are decompiles of both methods from Reflector. Just wondering why the .NET team would do something on one method and not on another method.
public virtual void Write(object o, string category)
{
if ((this.Filter == null) ||
this.Filter.ShouldTrace(null, "", TraceEventType.Verbose, 0, category, null, o))
{
if (category == null)
{
this.Write(o);
}
else
{
this.Write((o == null) ? "" : o.ToString(), category);
}
}
}
and then the string method.
public virtual void Write(string message, string category)
{
if ((this.Filter == null) ||
this.Filter.ShouldTrace(null, "", TraceEventType.Verbose, 0, message))
{
if (category == null)
{
this.Write(message);
}
else
{
this.Write(category + ": " + ((message == null) ? string.Empty : message));
}
}
}
The call to
this.Writein the first overloaddelegates the actual output to the second overload
All of the formatting for the output is then centralized in the second overload:
By centralizing the actual formatting in this way, changing the formatting in a single place will change all trace output formatting. It’s an application of DRY principles.