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.