DEV Community

Obinna Isiwekpeni
Obinna Isiwekpeni

Posted on

Log tracing in .NET Framework

Logging is a very vital part in Software development. It is used to record and trace error states in the development process. It helps to investigate errors after a problem has occurred. The recording can be done in the command line, a log file, an event log, a specialised database or a web service.

Tracing is the special use of logging to record information about a program’s execution. The information is used by programmers for debugging and depending on the details contained in a trace log can be used to diagnose common problems with software.

In this article, I am going to show how tracing logs in .NET framework works which uses the TraceSource class. The basis of tracing is to trace messages that are sent to listeners using switches that forward the data to the respective assigned output medium which could be a log file.

To demonstrate this, let's create a simple console application that runs a Powershell script. The following steps show how tracing works with snapshots and code examples.

  • Create the Powershell script: The script simply gets some processes running on a computer system using the Get-Process cmdlet.
Enter fullscreen mode Exit fullscreen mode
  • Create a new console project in Visual Studio using the .NET framework and C# programming language. Check out this tutorial for details.

  • Create an instance of the TraceSource class with the parameter “LogTracing” which is the name of the Tracesource.

private static TraceSource mySource = new TraceSource("LogTracing")
Enter fullscreen mode Exit fullscreen mode
  • Write the process for running the powershell script from the console application. The code snippet below shows how to do it. The parameters “RedirectStandardError” and “RedirectStandardOutput” determine if the errors and output respectively should be redirected to another location e.g. a log file. The "DataReceivedEventHandler" handles the events that are being received and passes it to the process that is considered i.e., output or error data.
static void RunScript()
 var psFile =Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, @"log_tracing.ps1");
            if (File.Exists(psFile))
                var startInfo = new ProcessStartInfo()
                    FileName = "powershell.exe",
                    Arguments = $"-ExecutionPolicy unrestricted -file \"{psFile}\"",
                    UseShellExecute = false,
                    Verb = "runas",
                    RedirectStandardError = true,
                    RedirectStandardOutput = true
                using(Process myprocess = Process.Start(startInfo))
                    myprocess.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                    myprocess.ErrorDataReceived += new DataReceivedEventHandler(ErrorHandler);
Enter fullscreen mode Exit fullscreen mode
  • The ErrorHandler and OutputHandler methods are shown in the snippet below. The TraceSource class has a TraceEvent method that writes a trace message to a trace listener. The trace listener will be added in the App.config file.
private static void ErrorHandler(object sender, DataReceivedEventArgs e)
  // write the error text to the file if there is something to write
   if (!String.IsNullOrEmpty(e.Data))
      mySource.TraceEvent(TraceEventType.Error, 2, e.Data);

private static void OutputHandler(object sender, DataReceivedEventArgs e)
  // write the output text to the file if there is something to write
 if (!String.IsNullOrEmpty(e.Data))
   mySource.TraceEvent(TraceEventType.Information, 1, e.Data);
Enter fullscreen mode Exit fullscreen mode
  • In the App.Config file, the trace listener is added and also the TraceSource name "LogTracing". The switchName and switchType are defined. The listener with the name "LogTracingListener" is added. There are 4 TraceLevel enumerations which are Verbose, Warning, Information and Error. You can find more information in the docs. Focus is on "Information" in this article.
      <source name="LogTracing"
          <add name="LogTracingListener"/>
          <remove name="Default"/>
      <add name="sourceSwitch" value="Information"/>
      <add name="LogTracingListener"
        <filter type="System.Diagnostics.EventTypeFilter"
Enter fullscreen mode Exit fullscreen mode
  • Run the code and you will find the log file in the bin folder of the project.

Please check the Microsoft docs for more info. Also, check the repo for the code.

Thanks for reading!

Top comments (0)