Friday, July 20, 2012

ASP.Net - How to Log Debug Information


The System.Diagnostics namespace provides a lot of tools for debugging. In this set, the most simple and frequently used tool is Debug.Write or Debug.WriteLine. These methods are used to print some text on the output window for debugging purpose.

Since Visual Studio provides a lot of rich debugging tools, it seems that people are not doing much experiment on this namespace. However, in some situations - especially on the production environment, I believe the System.Diagnostics is the best one to go with.

Just write the stuffs into Debug.Write or Debug.WriteLine where required and place the following into your Web.config file.

<system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="textListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="./Debug.log" />
      </listeners>
    </trace>
</system.diagnostics>

WoW!!! we are done with it. Its time to run the application and check the log now.

You can find the file Debug.log on the root of your project(of-course you can change the location  just by specifying it on initializeData attribute of the listeners configuration).

Well, this is not the only way to trace, track  or debug the application on production environment. But this is a simple approach that I have come across with.

If you would like to know more about this. Please refer the below links:
  1. Integrating ASP.NET Tracing with System.Diagnostics Tracing
  2. Configuring Tracing

No comments:

Post a Comment