Working with Trace & Log in c#

Download Demo Console Application Here

In many applications we need to trace various event & errors.
The .NET Framework contains a mechanism called a Trace Listener. This Listener is an object which directs the trace output to an appropriate target, such as an error log for example.

Lets create a console application to understand Tracing.

First we need to configure app.config / web.config for this

  <system.diagnostics>
    <sources>
      <source name="TraceSourceApp" switchValue="All">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="log.svclog"/>
    </sharedListeners>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <clear/>
        <add name="xml"/>
      </listeners>
    </trace>
  </system.diagnostics>

Here we are adding Source named “TraceSourceApp” with switchValue All”.

SwithValue defines the tracing level. The available tracing levels are:
1. Off
2. Critical
3. Error
4. Warning
5. Information
6. Verbose
7. ActivityTracing
8. All

For more information on Tracing level Refer

Here we are using tracing level “All” as we want to trace all the events of the application.
Also we can see that this source has listener named “xml”.

As you can see, we are adding a listener of type System.Diagnostics.TextWriterTraceListener which is set to write to a log file called log.svclog (here we can also create log.log file instead of log.svclog).

Now in static main class we need to create readonly source of class “TraceSource” with source defined in app.config (In our case it is “TraceSourceApp”)

private static readonly TraceSource TraceSource = new TraceSource("TraceSourceApp");

Now in your main method you need to trace the event like this

TraceSource.TraceEvent(TraceEventType.Start,0,"Trace Start Message");

1st parameter TraceEventType identifies the type of event that has caused the trace.
For more information please refer
Bellow is demo code of main class.
2nd parameter is event identifier id.
3rd parameter message whic has to shown.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace TraceLog
{
    internal class Program
    {
       
        private static readonly TraceSource TraceSource = new TraceSource("TraceSourceApp");

            static void Main (string[] args)
            {
                TraceSource.TraceEvent(TraceEventType.Start,0,"Trace Start Message");
                TraceSource.TraceEvent(TraceEventType.Error, 1,
                                    "Error message.");
                TraceSource.TraceEvent(TraceEventType.Warning, 2,
                                    "Warning message.");
                TraceSource.TraceEvent(TraceEventType.Information,3,"Information Message.");
                TraceSource.TraceEvent(TraceEventType.Stop,4,"Trace Stop Message");
               
                TraceSource.Close();
            }
    }
}

After running the console application you will get result something like bellow.

Download full source code Here

admin