Jun
17

Logging Messages in Dot Net

posted on 17 June 2010 in programming

Warning: Please consider that this post is over 13 years old and the content may no longer be relevant.

The .Net Framework provides an easy way to log messages using the System.Diagnostics.Trace class. By using Trace the user can configure where and what to log in the config file.

Below are two simple ways to log a message to a text file.

  1. The simplest way if you don’t care about the format of the message is to use a filter on the trace listener in app.config to determine which events to log.
<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add name="LogListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="trace.log" traceOutputOptions="DateTime">
        <!--
        EventTypes:
        Critical: Fatal error or application crash.
        Error: Recoverable error.
        Warning: Noncritical problem.
        Information: Informational message.
        Verbose: Debugging trace.
        Start: Starting of a logical operation.
        Stop: Stopping of a logical operation.
        Suspend: Suspension of a logical operation.
        Resume: Resumption of a logical operation.
        Transfer: Changing of correlation identity.
        -->
        <filter type="System.Diagnostics.EventTypeFilter" initializeData="Error"/>
      </add>
    </listeners>
  </trace>
</system.diagnostics>

Then you only need to call one of the following methods to log a message with a specific EventType.

System.Diagnostics.Trace.TraceInformation("Information messge");
System.Diagnostics.Trace.TraceError("Error messge");
  1. If you want more control over the format of the message you can use a TraceSwitch in app.config:
<system.diagnostics>
  <switches>
    <clear/>
    <!--
    Trace Levels:
    Off: 0: None
    Error: 1: Only error messages
    Warning: 2: Warning messages and error messages
    Info: 3: Informational messages, warning messages, and error messages
    Verbose: 4: Verbose messages, informational messages, warning messages, and error messages
    -->
    <add name="LogLevel" value="Info"/>
  </switches>
  <trace autoflush="true">
    <listeners>
      <add name="LogListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="trace.log"/>
    </listeners>
  </trace>
</system.diagnostics>

Then retrieve the TraceSwitch in code as follows:

TraceSwitch logLevel = new TraceSwitch("LogLevel", "The logging level");
Trace.WriteLineIf(logLevel.Level >= level, string.Format("[{0}] [{1}]: {2}", DateTime.Now, level, message));

The examples are for a desktop application, but they would work equally well in a web application by configuring web.config. Note that web applications aren’t compiled with Trace enabled, so you need to enable them in web.config by using the following after the <system.diagnostics> node:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp"
      extension=".cs"
      compilerOptions="/d:TRACE"
      type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="1" />
    </compilers>
</system.codedom>