2

I am curious about the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="logger" type="System.Diagnostics.TextWriterTraceListener"
             initializeData="LoggingFile.txt" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

I want to include the option in my solution to log error messages and the stack trace. But I need to be able to turn this on and off. Is this possible by commenting out everything in System.Diagnostics? Or is there a better way?

Is it possible to specify what file the logger writes to at run-time?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Hans Rudel
  • 3,433
  • 5
  • 39
  • 62
  • 1
    I realise that this was answered a long time ago, but I feel that the actual question of how to turn tracing off at runtime was not clearly answered. The simplest way is to add a switch in the config (named `SourceSwitch` in the answer example), manually changing its value to `Off` and then restarting the application or calling `Trace.Refresh` in code. – Sheridan Mar 13 '16 at 15:50

1 Answers1

4

You can make use of a SourceSwitch which allows you to trace different levels of messages. First start by adding some settings to app.config

<system.diagnostics>
  <sources>
    <source name="TraceTest" switchName="SourceSwitch"
            switchType="System.Diagnostics.SourceSwitch">
      <listeners>
            <!-- choose one or use multiple TraceListeners -->
            <add name="console" type="System.Diagnostics.ConsoleTraceListener"
                 initializeData="false"/>
            <add name="file" type="System.Diagnostics.TextWriterTraceListener"
                 initializeData="error.log"/>
            <remove name ="Default"/>
      </listeners>
    </source>
   </sources>
   <switches>
    <!--  MSDN: 4 verbose Information, Information 3, Warning 2, Error 1, -->
    <add name="SourceSwitch" value="Error"/>
  </switches>
  <trace autoflush="true" indentsize="4"/>
</system.diagnostics>

Within your application you can trace messages by using a TraceSource object referring to the definied name within app.config

TraceSource ts = new TraceSource("TraceTest");
ts.TraceEvent(TraceEventType.Information, 123, "event info");
ts.TraceEvent(TraceEventType.Error, 123, "event error");
ts.TraceEvent(TraceEventType.Warning, 123, "event warning");

ts.TraceInformation("any text");
ts.Flush();
ts.Close();

For some common information have a look at How to: Use TraceSource ... at MSDN. Using Dr. Google I found an related question at SO referring to a very good blogpost concerning this matter.

One thing I'd like to point out ...

To change the level at which a listener writes a trace message

The configuration file initializes the settings for the trace source at the time the application is initialized. To change those settings you must change the configuration file and restart the application or programmatically refresh the application using the TraceRefresh method. The application can dynamically change the properties set by the configuration file to override any settings specified by the user. For example, you might want to assure that critical messages are always sent to a text file, regardless of the current configuration settings.

Community
  • 1
  • 1
Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
  • Thanks very much for your answer. U havent mentioned how this can be turned on/off but it may be in one of the links u included so i have still marked ur answer as the selected answer. Will have a mess around with what u have mentioned above n see if i can get to grips with it. Thanks again for your help. – Hans Rudel Sep 13 '12 at 19:48