Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1024151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:44:11+00:00 2026-05-16T11:44:11+00:00

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

  • 0

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 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:44:12+00:00Added an answer on May 16, 2026 at 11:44 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.