Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Trace within Global.asax

I'm struggling to output the messages written via Trace.WriteLine within the Global.asax, they don't appear in the Trace.axd.

I've added a WebPageTraceListener and a TextWriterTraceListener as documented here but all I see are the normal page events you would see in a trace which is expected.

Am I missing a step in order to get trace messages written in the Global.asax to the file/trace log? I'm doing some logging in the Application_AuthenticateRequest event.

like image 553
Naeem Sarfraz Avatar asked Nov 11 '11 06:11

Naeem Sarfraz


1 Answers

Did you compile using the TRACE switch or update your web.config to do so automatically?

From the MSDN page you linked to (emphasis mine):

Although ASP.NET displays trace messages whenever tracing is enabled for a page, System.Diagnostics trace messages are written only when the code in which the trace messages reside is compiled by using an explicit compiler switch—the TRACE switch. In other words, if you do not explicitly compile the AuthorClass using the TRACE switch, you will not see the trace messages, even with the WebPageTraceListener added.

You can configure your application to automatically compile using the TRACE switch, by adding a new section to your Web.config file.

This is the Web.config entry that should be placed after the <system.diagnostics> section:

<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" />
    <compiler language="VB"
              extension=".vb" 
              compilerOptions="/d:Trace=true"
              type="Microsoft.VisualBasic.VBCodeProvider, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </compilers>
</system.codedom>
like image 146
Ahmad Mageed Avatar answered Oct 11 '22 20:10

Ahmad Mageed