如何使ASP.NET MVC跟踪信息与页面跟踪输出一致,作为trace.axd?我可能只是缺少一些明显的东西,如果你看到它,请打电话给它。
传统ASP.NET的背景信息
所以回到常规的ASP.NET日子,你可以简单地添加以下到你的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,PublicKeyToken=b77a5c561934e089">
HttpContext.Current.Trace.Write("Write Trace Here"); or System.Diagnostics.Trace.Write("Write Trace Here");
如果你打到你的页面(即localhost:61115 / Default.aspx),你会得到一个漂亮的跟踪表,你的自定义跟踪嵌入asp.net页面事件:
aspx.page Begin Load 0.00343 0.000357 test 0.00462176 0.001192 aspx.page End Load 0.00526904 0.000018
点击本地主机:61115 / Trace.axd?id = 0将保持与页内跟踪输出相同的跟踪结果。
ASP.NET MVC背景信息
不幸的是,由于我不知道的原因,我无法让它在ASP.NET MVC 2.0中工作。我使用类似的web.config设置,如上所列。有趣的是,我只能获得部分工作的痕迹。所以如果我打了等效的默认页面(也就是我的homecontroller的索引操作方法)的功能,我看到所有传统的asp.net页面事件,如preinit,preload,prerender等,但没有自定义跟踪msg使用System.Diagnostics或HttpContext.Trace.Write。
但是,如果我转到Trace.axd?id = 0文件,我遇到了我的自定义跟踪消息,但没有ASP.NET页面事件跟踪输出。我必须在这里遗漏一些这样的跟踪信息不一致的信息,我看到页面内容与trace.axd(回想一下,传统的asp.net输出的页面与trace.axd输出相同)。我真的希望让我的页内跟踪信息与trace.axd一致(通过删除传统的asp.net页面事件或其他方式)。有没有我失踪的东西?
解决方法
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.
对不起我用自己的话来回答,但是我觉得这个解释是在:)