Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Tracing Issues

Question

How do I get ASP.NET MVC trace information to be consistent for in-page trace output as trace.axd? I may just be missing something obvious, please call it out if you see it.

Background Info for Traditional ASP.NET

So back in the regular ASP.NET days, you could simply add the following to your web.config:

<system.diagnostics>
    <trace>
        <listeners>
            <add name="WebPageTraceListener" type="System.Web.WebPageTraceListener, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </listeners>
    </trace>
</system.diagnostics>
...
<system.web>
    <trace enabled="true" pageOutput="true" writeToDiagnosticsTrace="true"/>
...
<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" warningLevel="1" compilerOptions="/d:TRACE" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

Then you could add the following in your pages:

HttpContext.Current.Trace.Write("Write Trace Here");
or
System.Diagnostics.Trace.Write("Write Trace Here");

And if you hit your page (i.e. localhost:61115/Default.aspx), you would get a nice trace table with your custom trace embedded with asp.net page events:

aspx.page Begin Load 0.00343 0.000357
test 0.00462176 0.001192 
aspx.page End Load 0.00526904 0.000018 

Hitting localhost:61115/Trace.axd?id=0 would retain the same trace results as the in-page trace outputs.

Background Info on ASP.NET MVC

Unfortunately, I cannot get this to work in ASP.NET MVC 2.0 for reasons unknown to me. I use similar web.config settings as ones listed above. What is interesting is that I can only get traces to partially work. So if I hit the functionality equivalent default page (aka index action method of my homecontroller), I see all of the traditional asp.net page events such as preinit, preload, prerender, etc but no custom trace msg using either System.Diagnostics nor HttpContext.Trace.Write.

However, if I turn to the Trace.axd?id=0 file, I'm greeted with my custom trace messages but no ASP.NET page event trace output. I must be missing something here that is causing this inconsistency in trace information I see in-page vs. trace.axd (recall that traditional asp.net outputs identical in-page vs. trace.axd outputs). I would really like to have my in-page trace information consistent with trace.axd (either by removing the traditional asp.net page events or other means). Is there something I am missing?

like image 876
user401435 Avatar asked Jul 25 '10 09:07

user401435


People also ask

What is tracing in ASP.NET MVC?

ASP.NET tracing enables you to follow a page's execution path, display diagnostic information at run time, and debug your application. ASP.NET tracing can be integrated with system-level tracing to provide multiple levels of tracing output in distributed and multi-tier applications.

Which file gets added when tracing is enabled in the application?

When you enable tracing for the whole application in the Web. config file, trace information is gathered and processed for each page in that application.

What is trace in C#?

Tracing helps to see the information of issues at the runtime of the application. By default Tracing is disabled. Tracing has the following important features: We can see the execution path of the page and application using the debug statement. We can access and manipulate trace messages programmatically.


1 Answers

Quoting directly from the book "MVC 2 in action":

When you called Trace.Write() in Web Forms, you were interacting with the Trace- Context class. This exists on your ViewPage in ASP.NET MVC, but this isn’t where you would want to write tracing statements. By the time you’ve passed the baton over to the view, there’s no logic there that you’d need to trace. Instead, you’d like to trace the logic embedded in your controllers. You might try to leverage the TraceContext class in your controller, but these statements won’t ever make their way to the list of messages in the trace log (on your page or on Trace.axd). Instead, you can use System.Diagnostics.Trace and set up your own TraceListeners to inspect the activity in your controllers. Alternatively, you can leverage a more mature logging framework such as log4net or NLog:

You debug ASP.NET MVC applications just as you would any .NET application. Tracing, however, doesn’t offer as much for MVC. Instead, you can lean on the built-in TraceListeners in .NET, or utilize a good logging library like those mentioned earlier. Another aspect of error logging is health monitoring.

Sorry for not answering in my own words, but I think this explanation is spot on :)

like image 116
psousa Avatar answered Sep 21 '22 14:09

psousa