Add Trace methods to System.Diagnostics.TraceListener

前端 未结 2 2046
渐次进展
渐次进展 2021-01-21 07:40

I wrote a Log class derived from System.Diagnostics.TraceListener like so

public class Log : TraceListener

This acts as a wrapper to Log4Net an

相关标签:
2条回答
  • 2021-01-21 08:18

    Your problem is that you cannot add extension methods to act on static classes. The first parameter of an extension method is the instance that it should operate on. Since the Trace class is static, there is no such instance. You are probably better off creating your own static log wrapper, exposing the methods you want to have, such as TraceVerbose:

    public static class LogWrapper
    {
        public static void TraceVerbose(string traceMessage)
        {
            Trace.WriteLine("VERBOSE: " + traceMessage);
        }
        // ...and so on...
    }
    

    This will also decouple you logging code from the actual logging framework in use, so that you can later switch from trace logging to using log4net or something else, should you wish to.

    0 讨论(0)
  • 2021-01-21 08:30

    This is way late, but have you considered using TraceSource? TraceSources give you actual object instances that you can use to log to System.Diagnostics (which means that you could extend them with an extension method as you propose in your question). TraceSources are typically configured in app.config (similar to how you would configure log4net loggers). You can control the level of logging and which trace listeners are listening. So, you could have application code, programmed against TraceSource, that might look something this:

    public class MyClassThatNeedsLogging
    {
      private static TraceSource ts = 
              new TraceSource(MethodBase.GetCurrentMethod().DeclaringType.Name);
      //Or, to get full name (including namespace)
      private static TraceSource ts2 = 
              new TraceSource(MethodBase.GetCurrentMethod().DeclaringType.FullName);
    
      private count;
    
      public MyClassThatNeedsLogging(int count)
      {
        this.count = count;
    
        ts.TraceEvent(TraceEventType.Information, 0, "Inside ctor.  count = {0}", count);
      }
    
      public int DoSomething()
      {
        if (this.count < 0)
        {
          ts.TraceEvent(TraceEventType.Verbose, 0, "Inside DoSomething.  count < 0");
          count = Math.Abs(count);
        }
    
        for (int i = 0; i < count; i++)
        {
          ts.TraceEvent(TraceEventType.Verbose, 0, "looping.  i = {0}", i);
        }
      }
    }
    

    You can also create TraceSources using any name (i.e. it doesn't have to be the class name):

    TraceSource ts1 = new TraceSource("InputProcessing");
    TraceSource ts2 = new TraceSource("Calculations");
    TraceSource ts3 = new TraceSource("OutputProcessing");
    

    As I mentioned earlier, each TraceSource is typically configured in the app.config file, along with the logging "level" and the listener that should receive the output.

    For your extension method, you could do something like this:

    public static class TraceSourceExtensions
    {
      public static void TraceVerbose(this TraceSource ts, string message)
      {
        ts.TraceEvent(TraceEventType.Verbose, 0, message);
      }
    }
    

    If you need to do more customization of TraceSource (like add additional levels), this is a pretty good article that describes how to do that:

    http://msdn.microsoft.com/en-us/magazine/cc300790.aspx

    If you are ultimately using log4net inside of your TraceListener (and using it to define named loggers, logging levels, etc), you might not need to configure many TraceSources. You might even be able to configure only one (whose name would be well-known) or you might be able to create one programmatically, set it to log "all", and hook it up to your specific TraceListener.

    In the end, instead of logging through the static Trace object, you can log through TraceSource instances. If there is one TraceSource configured and its name is well known, a valid TraceSource can be created (for logging) anywhere like this:

    TraceSource ts = new TraceSource("logger");
    ts.TraceEvent(TraceEventType.Information, 0, "Hello World!");
    //Or, via your extension method
    ts.TraceVerbose(TraceEventType.Verbose, 0, "Logged via extension method");
    

    There may be better ways to accomplish what you are trying to accomplish, but this might give you something to think about regarding using TraceSource vs the static Trace class.

    0 讨论(0)
提交回复
热议问题