2013-01-02 81 views
3

我们开发了我们自定义的HttpModule。
现在我想添加跟踪它的能力,并查看标准ASP.NET跟踪页面(或trace.axd)中的跟踪结果。我尝试使用System.Diagnostics.Trace.Write("FILTER TEST");来写入跟踪信息。除了HttpModule之外,这个地方可以工作。我在web.config中添加了一个跟踪监听器,但它只显示在页面生命周期中写入的跟踪。我如何看到我在HttpModule中编写的跟踪信息以及如何将这些信息添加到ASP.NET跟踪页面?ASP.NET跟踪 - 自定义HttpModule

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

回答

0

我通常使用TraceSource,即使在自定义的HttpModule中它也能正常工作。

所有你需要的是:

  • 声明的来源(如你在你的代码想,你可以使用尽可能多的资源,这取决于你想要什么的粒度在你追踪) - (更多信息在TraceSource here):

    public class MyModule : IHttpModule 
    { 
        public static readonly TraceSource MyTraceSource = new TraceSource("MySource", SourceLevels.Off); 
    (...) 
    } 
    
  • 在你的代码,当你需要你的源上traceEvent方法here)跟踪(更多信息转储东西使用TraceEvent:

    MyModule.MyTraceSource.TraceEvent(TraceEventType.Information, 0, "Message that'll be dumped in the trace"); 
    
  • 在您的配置文件,您可以启用/禁用只有你想在你想要的水平tracesource(信息,警告,错误,...)

    <system.diagnostics> 
    
        <trace autoflush="true"/> 
    
        <sharedListeners> 
         <add name="myListener" initializeData="..." type="..."/> 
        </sharedListeners> 
    
        <sources> 
         <source name="MySource" switchName="VerboseSwitch" > 
          <listeners> 
           <clear/> 
           <add name="myListener"/> 
          </listeners> 
         </source> 
         <source name="OtherSource" switchName="OffSwitch" > 
          <listeners> 
           <clear/> 
           <add name="myListener"/> 
          </listeners> 
         </source> 
        </sources> 
    
        <switches> 
         <clear/> 
         <add name="OffSwitch" value="Off"/> 
         <add name="VerboseSwitch" value="Verbose"/> 
         <add name="InformationSwitch" value="Information"/> 
         <add name="WarningSwitch" value="Warning"/> 
         <add name="ErrorSwitch" value="Error"/> 
        </switches> 
    
    </system.diagnostics> 
    

在这里,您可以找到一个非常好的文章上的痕迹主题:http://www.codeproject.com/Articles/149251/Debugging-Tracing-and-Instrumentation-in-NET-and-A

希望这会有所帮助!