C#.NET Suppress Trace Messages From Specific "noisy" DLL

We've recently had a problem with the .NET diagnostics trace listeners.

These allow you to easily write diagnostics information to, for example, a text file.

However these are static methods so when you use 3rd party DLLs you may find that your nice simple trace messages suddently become "hijacked" by a "noisy" DLL.

It's very hard to separate the source of the Trace.WriteLine() call without pulling up stack traces which can impact performace.

Here's a simple workaround where you can use the category to exclude messages from elsewhere.

Create a method which calls Trace.WriteLine() for you which passes a GUID so you know it has been called from your class.

/// <summary>
///
Provides the ability to log diagnostics information to the system trace log.
///
</summary>
public
class DiagnosticsSupport

{

    /// <summary>

    /// Writes the specified message to the diagnostics trace log.   
    ///
</summary>
    ///
<param name="message">The message to be written to the log.</param>
    /// <param name="includeTimeStamp">Determines if the message should be written with a timestamp, by default this is true.</param>   
    public
static void Log(String message, bool includeTimeStamp)
    {

        message = StringSupport.CleanString(message);

        String longTimeString = String.Format(
"{0:HH:mm:ss}", DateTime.Now);
        String timedMesssage = String.Format(
"{0} {1}.{2}: {3}", DateTime.Now.ToShortDateString(), longTimeString, DateTime.Now.Millisecond, message)
        Trace.WriteLine(includeTimeStamp ? timedMesssage : message, "8aaa0a9a-8094-4cc6-af98-e3216a39d3bb");
        Console.WriteLine(message);
   
    }

}


Overwride the appropriate method and check if the GUID has been passed, otherwise do not write the message to the listener.

Override all of the other methods and ignore them, simply returning.

using System.Diagnostics;
using
System.IO;

namespace CENTREL.XIA.Support
{

 

    /// <summary>
    
/// Represents an advanced text writer trace listener.
    
/// </summary>
    
public class AdvancedTextWriterTraceListener: TextWriterTraceListener   
    
{

        /// <summary>
        
/// Initializes a new instance of the CENTREL.XIA.Support.AdvancedTextWriterTraceListener class.
        
/// </summary>
        
public AdvancedTextWriterTraceListener()       
        {

        }

 

 

        /// <summary>       
        ///
Initializes a new instance of the CENTREL.XIA.Support.AdvancedTextWriterTraceListener class using the specified writer as recipient of the tracing or debugging output.

        /// </summary>       
        ///
<param name="writer">A System.IO.TextWriter that receives the output from the System.Diagnostics.TextWriterTraceListener.</param>

        public AdvancedTextWriterTraceListener(TextWriter writer):

            base(writer)

        {

 

        }

 

        /// <summary>       
        ///
Writes a message to the listener when the category matches the well known GUIDB "8aaa0a9a-8094-4cc6-af98-e3216a39d3bb". Only the message is written, the category is ignored.       
        ///
</summary>
        
/// <param name="message">The message to write to the listener.</param>       
        ///
<param name="category">The category name or well known GUID "8aaa0a9a-8094-4cc6-af98-e3216a39d3bb".</param>

        public override void WriteLine(string message, string category)     
        {

            if (!StringSupport.Equals(category, "8aaa0a9a-8094-4cc6-af98-e3216a39d3bb")) { return; }

            base.WriteLine(message);
        }
 

    }

 

}

 

Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Enable function lock for F1-F12 on HP ZBook mobile workstations