Posts Tagged ‘debugging’

Maintain debug information while in release mode

Posted on: (1 Comment

If you have every deployed a website / web application, you would most probably have included some kind of logging mechanism that records any exceptions that occur at runtime. In my case, I like to include a global exception catcher in my apps so that I get notified if a user receives any kind of uncaught exception. The main problem with this approach is that if the application is compiled in release mode you normally would not be able to get an exact handle on the line of code that caused the exception. The trace is still useful but would be much more valuable if the line numbers were included.

Well, this is possible even when your app is running in release mode. Simply update your web.config as follows (note the highlighted compilerOptions tag):

  141 <system.codedom>

  142     <compilers>

  143         <compiler language=”c#;cs;csharp” extension=”.cs” warningLevel=”4” type=”Microsoft.CSharp.CSharpCodeProvider, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089compilerOptions=”/debug:pdbonly>

  144             <providerOption name=”CompilerVersion” value=”v3.5“/>

  145             <providerOption name=”WarnAsError” value=”false“/>

  146         </compiler>

  147         <compiler language=”vb;vbs;visualbasic;vbscript” extension=”.vb” warningLevel=”4” type=”Microsoft.VisualBasic.VBCodeProvider, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089compilerOptions=”/debug:pdbonly>

  148             <providerOption name=”CompilerVersion” value=”v3.5“/>

  149             <providerOption name=”OptionInfer” value=”true“/>

  150             <providerOption name=”WarnAsError” value=”false“/>

  151         </compiler>

  152     </compilers>

  153 </system.codedom>

For more information about emitting debug information follow this link:

Hope this helps.

Logging trace output

I was recently testing out a third party tool and wanted to log some of the trace information, here is how you do it:

* Note that in this example, the trace is simply being sent to the console, but you could write it to a file / database …

    1 // Place the code below at the beginning of your execution context

    2 StringBuilder builder = new StringBuilder();

    3 TextWriter writer = new StringWriter(builder);

    4 TextWriterTraceListener listner = new TextWriterTraceListener(writer);

    5 Trace.Listeners.Add(listner);


    7 // Then do something here



   10 // Place the code below at the end of your execution context to log to file / database / console …

   11 Console.Write(builder.ToString());