Ian Boyd
Ian Boyd

Reputation: 257113

How to trace Debug messages in ASP.net?

Short version: i've added

 <system.web>
   <system.diagnostics>
      <trace>
         <listeners>
            <add name="WebPageTraceListener" type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
         </listeners>
      </trace>
   </system.diagnostics>

to my web-site's web.config file, but Visual Studio complains:

Unrecognized configuration section system.web/system.diagnostics.

How do i make Visual Studio (2010 (Professional (Windows 7 (64-bit)))) not complain?


Background

Microsoft explains that in ASP.net you use the TraceContext.Warn method to add tracing information to the trace output, e.g.:

Page.Trace.Warn("Starting up - da shield");

Tracing is then enabled when you enable tracing:

<configuration>
  <system.web>
    <trace enabled="true" requestLimit="40" localOnly="false" />
  </system.web>
</configuration>

or when you enable tracing:

<%@ Page ... Trace="true" ... %>

Tracing is then enabled on the bottom of the page:

enter image description here


But what about System.Diagnostics tracing?

That's well and good for ASP.net code, but business objects don't use System.Web.UI.TraceContext for tracing, they use System.Diagnostics.Trace:

System.Diagnostics.Trace.TraceWarning("No giving up General Jar Jar. Yousa tink of someting");

Microsoft explains that you can route Diagnostics tracing through Web tracing using:

Walkthrough: Integrating ASP.NET Tracing with System.Diagnostics Tracing

Routing All Tracing Output to the Web Form

To route Trace messages to an ASP.NET Web page, you must add a WebPageTraceListener object.

Place the following code in your Web.config file after the <system.web> section.

<system.diagnostics>
  <trace>
    <listeners>
       <add name="WebPageTraceListener" 
            type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </listeners>
  </trace>
</system.diagnostics>

Except that Visual Studio complains:

Unrecognized configuration section system.web/system.diagnostics.

Worse Than Failure?

Upvotes: 2

Views: 4864

Answers (1)

misha
misha

Reputation: 2889

<system.diagnostics> should be the child of <configuration> not <system.web>, so something like:

<configuration>
...
 <system.diagnostics>
  <trace>
    <listeners>
       <add name="WebPageTraceListener" 
            type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </listeners>
  </trace>
</system.diagnostics>
</configuration>

Hope I understood the issue right and this helps.

Upvotes: 3

Related Questions