2013-01-02 80 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

希望這會有所幫助!