views:

249

answers:

2

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

public class Log : TraceListener

This acts as a wrapper to Log4Net and allows people to use System.Diagnostics Tracing like so

Trace.Listeners.Clear();
Trace.Listeners.Add(new Log("MyProgram"));
Trace.TraceInformation("Program Starting");

There is a request to add additional tracing levels then the default Trace ones (Error,Warning,Information)

I want to have this added to the System.Diagnostics.Trace so it can be used like

Trace.TraceVerbose("blah blah");
Trace.TraceAlert("Alert!");

Is there any way I can do this with an extension class? I tried

public static class TraceListenerExtensions
{
     public static void TraceVerbose(this Trace trace) {}
}

but nothing is being exposed on the trace instance being passed in :(

A: 

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.

Fredrik Mörk
The problem then is everyone has to use my logger. By working within the logging framework, people can just use that. For instance System.Diagnostics.Trace.TraceWarning("warning!"). Also, any third party assemblies using System.Diagnostics.Trace will automatically be sending messages to our logger with no modification.
Kenoyer130
A: 

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.

wageoghe