sfm sfm - 2 years ago 88
C# Question

Suppress Trace Messages From Specific DLL

I'm using a 3rd party library which makes several calls to the function:

Trace.WriteLine(string message);

This clutters up the visual studio output window and makes debugging my application difficult (for instance; XAML binding warnings).

I'm trying to find a way to stop all trace messages from a specific dll from dumping to the visual studio output window - is writing my own TraceListener the only path forward?

I can't make a TraceFilter / EventTypeFilter work for a string message without category -- although I can't find the documentation to back this up -- empirically:


is called by the following functions (not a complete set):

Trace.WriteLine(string message, string category);
Trace.TraceError(string message);
Trace.WriteLine(object o);

but isn't called by:

Trace.WriteLine(string message);

Does anyone know why this call avoids the ShouldTrace filter?

Answer Source

According to ILSpy, the Trace.WriteLine(string message) is declared as abstract and needs to be overridden by derived classes:

public abstract void WriteLine(string message);

All other methods you mention check the ShouldTrace and ultimately call the Trace.WriteLine(string message) message.


public virtual void WriteLine(string message, string category)
    if (Filter != null && 
        !Filter.ShouldTrace(null, "", TraceEventType.Verbose, 0, message))
    if (category == null)
    WriteLine(category + ": " + ((message == null) ? string.Empty : message));

So the real reason is in my opinion, a decision of the designer of the Trace class.

He could have made that Trace.WriteLine(string message) protected to incidate that it is not intended to be called directly, e.g.:

protected abstract void WriteLine(string message);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download