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

  • Home
  • SEARCH
  • 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 3390570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:42:25+00:00 2026-05-18T03:42:25+00:00

I’m trying to use System.Diagnostics to do some very basic logging. I figure I’d

  • 0

I’m trying to use System.Diagnostics to do some very basic logging. I figure I’d use what’s in the box rather than taking on an extra dependency like Log4Net or EntLib.

I’m all set up, tracing is working wonderfully. Code snippet:

Trace.TraceInformation("Hello World")

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="TraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Trace.log" traceOutputOptions="DateTime" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

and my little “Hello World” shows nicely up in my Trace.log file. But now I’d like to switch OFF tracing, so I dig into MSDN and find How to: Configure Trace Switches
. I add the <switches> element, and now my app.config looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="TraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Trace.log" traceOutputOptions="DateTime" />
        <remove name="Default" />
      </listeners>
    </trace>
    <switches>
      <add name="Data" value="0" />
    </switches>
  </system.diagnostics>
</configuration>

The value="0" should turn off tracing – at least if you then follow How to: Create and Initialize Trace Switches, which tells you to add this line of code:

Dim dataSwitch As New BooleanSwitch("Data", "DataAccess module")

That doesn’t make sense to me: I just have to declare an instance of the BooleanSwicth to be able to manage (disable) tracing via the .config file? Should I like … use … the object somewhere?

Anyways, I’m sure I missed something really obvious somewhere. Please help.

How do I switch OFF tracing in app.config?

  • 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-18T03:42:26+00:00Added an answer on May 18, 2026 at 3:42 am

    I agree with @Alex Humphrey’s recommendation to try using TraceSources. With TraceSources you gain more control over how your logging/tracing statements execute. For example, you could have code like this:

    public class MyClass1
    {
      private static readonly TraceSource ts = new TraceSource("MyClass1");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    
    public class MyClass2
    {
      private static readonly TraceSource ts = new TraceSource("MyClass2");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    

    The TraceSource.TraceEvent call will automatically check the level of the message (TraceEventType.Information) against the configured level of the associated Switch and will determine whether or not the message should actually be written out.

    By using a differently named TraceSource for each type, you can control the logging from those classes individually. You could enable MyClass1 logging or you could disable it or you could enable it but have it log only if the level of the message (TraceEventType) is greater than a certain value (maybe only log “Warning” and higher). At the same time, you could turn logging in MyClass2 on or off or set to a level, completely independently of MyClass1. All of this enabling/disabling/level stuff happens in the app.config file.

    Using the app.config file, you could also control all TraceSources (or groups of TraceSources) in the same way. So, you could configure so that MyClass1 and MyClass2 are both controlled by the same Switch.

    If you don’t want to have a differently named TraceSource for each type, you could just create the same TraceSource in every class:

    public class MyClass1
    {
      private static readonly TraceSource ts = new TraceSource("MyApplication");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    
    public class MyClass2
    {
      private static readonly TraceSource ts = new TraceSource("MyApplication");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    

    This way, you could make all logging within your application happen at the same level (or be turned off or go the same TraceListener, or whatever).

    You could also configure different parts of your application to be independently configurable without having to go the “trouble” of defining a unique TraceSource in each type:

    public class Analysis1
    {
      private static readonly TraceSource ts = new TraceSource("MyApplication.Analysis");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    
    public class Analysis2
    {
      private static readonly TraceSource ts = new TraceSource("MyApplication.Analysis");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    
    public class DataAccess1
    {
      private static readonly TraceSource ts = new TraceSource("MyApplication.DataAccess");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    
    public class DataAccess2
    {
      private static readonly TraceSource ts = new TraceSource("MyApplication.DataAccess");
    
      public DoSomething(int x)
      {
        ts.TraceEvent(TraceEventType.Information, "In DoSomething.  x = {0}", x);
      }
    }
    

    With your class instrumented this way, you could make the “DataAccess” part of your app log at one level while the “Analysis” part of your app logs at a different level (of course, either or both parts of your app could be configured so that logging is disabled).

    Here is a part of an app.config file that configures TraceSources and TraceSwitches:

    <system.diagnostics>
      <trace autoflush="true"></trace>
      <sources>
        <source name="MyClass1" switchName="switch1">
          <listeners>
            <remove name="Default"></remove>
            <add name="console"></add>
          </listeners>
        </source>
        <source name="MyClass2" switchName="switch2">
          <listeners>
            <remove name="Default"></remove>
            <add name="console"></add>
          </listeners>
        </source>
      </sources>
      <switches>
        <add name="switch1" value="Information"/>
        <add name="switch2" value="Warning"/>
      </switches>
      <sharedListeners>
        <add name="console"
             type="System.Diagnostics.ConsoleTraceListener">
        </add>
        <add name="file"
             type="System.Diagnostics.TextWriterTraceListener"
             initializeData="trace.txt">
        </add>
      </sharedListeners>
    </system.diagnostics>
    

    As you can see, you could configure a single TraceSource and a single Switch and all logging would occur with a single level of control (i.e. you could turn all logging off or make it log at a certain level).

    Alternatively, you could define multiple TraceSources (and reference the corresponding TraceSources in your code) and multiple Switches. The Switches may be shared (i.e. multiple TraceSources can use the same Switch).

    Ultimately, by putting in a little more effort now to use TraceSources and to reference appropriately named TraceSources in your code (i.e. define the TraceSource names logically so that you can have the desired degree of control over logging in your app), you will gain significant flexibility in the long run.

    Here are a few links that might help you with System.Diagnostics as you go forward:

    .net Diagnostics best practices?

    Logging best practices

    What's the best approach to logging?

    Does the .Net TraceSource/TraceListener framework have something similar to log4net's Formatters?

    In the links I posted, there is often discussion of the “best” logging framework. I am not trying to convince you to change from System.Diagnostics. The links also tend to have good information about using System.Diagnostics, that is why I posted them.

    Several of the links I posted contain a link to Ukadc.Diagnostics. This is a really cool add on library for System.Diagnostics that adds rich formatting capability, similar to what you can do with log4net and NLog. This library imposes a config-only dependency on your app, not a code or reference dependency.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.