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 8995493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:37:39+00:00 2026-06-15T23:37:39+00:00

You’d think Trace.TraceInformation/Warning/Error would respect the level set in a TraceSwitch Investigation reveals that

  • 0

You’d think Trace.TraceInformation/Warning/Error would respect the level set in a TraceSwitch

Investigation reveals that there can be any number of TraceSwitch instances and therefore it’s not clear which one ought to govern behaviour. There is a way to make a TraceSwitch instance take its level from config – you just specify a matching name when you create the instance.

If I’d designed things, the Trace class would have a static property TraceSwitch which would be set by the first trace switch definition in the config, and the TraceInfo etc methods would behave like this

    public static void TraceInformation(string message, params object[] p)
    {
      if (TraceSwitch.TraceInfo)
        //base behaviour
    }

This wasn’t hard to produce as a wrapper.

namespace System.Diagnostics
{
  public static class TraceEx
  {
    private static TraceSwitch __traceSwitch;
    public static TraceSwitch TraceSwitch
    {
      get { return __traceSwitch; }
      set { __traceSwitch = value; }
    }
    static TraceEx()
    {
      __traceSwitch = new TraceSwitch("Default", "Default trace level is Off");
    }
    public static void TraceError(string message, params object[] p)
    {
      if (TraceSwitch.TraceError)
        Trace.TraceError(message, p);
    }
    public static void TraceInformation(string message, params object[] p)
    {
      if (TraceSwitch.TraceInfo)
        Trace.TraceInformation(message, p);
    }
    public static void TraceWarning(string message, params object[] p)
    {
      if (TraceSwitch.TraceWarning)
        Trace.TraceWarning(message, p);
    }
    public static void Write(string message, params object[] p)
    {
      if (TraceSwitch.TraceVerbose)
        Trace.Write(string.Format(message, p));
    }
    public static void WriteLine(string message, params object[] p)
    {
      Write(message + "\r\n", p);
    }
  }
}

Putting it in the same namespace meant all I had to do was reference the project and rename Trace to TraceEx. The question I’m posing is this: why didn’t Microsoft do it like this? Is there some looming gotcha I haven’t seen?


Well, that was unexpected (see accepted answer). Which only goes to show I was right to suspect I hadn’t understood the grand design. All of the above turns into this:

<userSettings>
    <TraceFilterExperiment.Properties.Settings>
        <setting name="TraceLevel" serializeAs="String">
            <value>Warning</value>
        </setting>
    </TraceFilterExperiment.Properties.Settings>
</userSettings>

var eventTypeFilter = new EventTypeFilter(Properties.Settings.Default.TraceLevel);
foreach (TraceListener L in Trace.Listeners) 
  L.Filter = eventTypeFilter;

But I remain hazy on what exactly one would really do with TraceSwitch.

  • 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-06-15T23:37:40+00:00Added an answer on June 15, 2026 at 11:37 pm

    The behavior you’re suggesting already exists, albeit in a somewhat different form: you modify the listeners to enable the different error levels.

    With your suggested behavior, sending different levels to different places (for example, verbose information to a log file, but only critical errors to the console) would require that your default trace switch be set to “allow everything,” just as it is with the existing behavior. In other words, if a programmer wants to use Trace switches as they’re intended, he would have to create a dummy trace switch that allows everything, and somehow make sure that it’s the first trace switch defined. It’s quite possible, for example, that switches are defined in some other configuration file (i.e. not application.exe.config), and there’s no way to guarantee the order in which configuration files are loaded.

    The Trace class itself is not the place to enforce the policy that you’re suggesting. Trace exists to provide a common interface and a centralized point where trace messages can be sent and then distributed. It is the application’s job to determine (via trace switches) what information is sent, and it is the listeners’ job to determine whether that information is output.

    The behavior you propose makes things easier for one particular application of Trace. It makes it confusing, more difficult, and possibly inconsistent for all other applications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.