2010-09-29 28 views
5

我試圖設置.NET跟蹤。我能夠通過System.Diagnostics.Trace獲得基本跟蹤,但由於複雜的原因,我必須通過System.Diagnostics.TraceSource對象(自.NET 2.0以來的新方法)激活跟蹤,而不是使用System .Diagnostics.Trace。我嘗試了一切,但它只是不想使用TraceSource。我在ASP.NET代碼隱藏執行描跡(aspx.cs).NET跟蹤不能與Diagnostics.TraceSource一起使用,只有Diagnostics.Trace

下面是一些相關網址:

http://msdn.microsoft.com/en-us/library/ty48b824.aspx
http://msdn.microsoft.com/en-us/library/64yxa344.aspx
http://msdn.microsoft.com/en-us/library/sk36c28t.aspx
http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396431.aspx
http://msdn.microsoft.com/en-us/library/b0ectfxd%28v=VS.100%29.aspx

目前,根據web.config中的內容,它應該同時追蹤文件和頁面,從此代碼:

TraceSource ts = new TraceSource("mysource", SourceLevels.All); 
Trace.Write("Trace (old way)"); // this one works 
ts.TraceInformation("Trace (new way)"); // this one doesn't work 
ts.Flush(); 
ts.Close(); 

這裏的web.config中相關章節:

<system.diagnostics> 
     <trace autoflush="false"> 
      <listeners> <!-- these listeners activate the "old way" of tracing. --> 
       <add  name="pagelistener" /> 
       <add  name="filelistener" /> 
      </listeners> 
     </trace> 

     <sources> 
      <source name="mysource" switchName="myswitch"> 
       <listeners> <!-- these listeners activate the "new way" --> 
         <add name="pagelistener" /> 
         <add name="filelistener" /> 
       </listeners> 
      </source> 
     </sources> 


     <sharedListeners> 
      <!-- these are the actual trace listeners --> 
      <add 
        name="filelistener" 
       type="System.Diagnostics.TextWriterTraceListener" 
       initializeData="loplog.txt" 
       /> 
      <add 
       name="pagelistener" 
       traceOutputOptions="none" 
       type="System.Web.WebPageTraceListener, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
       /> 
     </sharedListeners> 

     <switches> 
      <!-- the sources above use this verbose switch --> 
      <add name="myswitch" value="Verbose"/> 
     </switches> 

</system.diagnostics> 
<system.codedom> 
     <!-- this compilers section should not be needed because I added 
       #define TRACE to the .aspx.cs file, however I put this in 
       since it's still not working. --> 

     <compilers> 
      <compiler 
          language="c#;cs;csharp" 
          extension=".cs" 
          compilerOptions="/d:TRACE" 
          type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
          warningLevel="1" 
          /> 
     </compilers> 
</system.codedom> 

<system.web> 
     <!-- this trace tag should be redundant because I added trace="true" to the aspx file, 
       but I put it in here anyway because this wasn't working. --> 
     <trace writeToDiagnosticsTrace="true" enabled="true" pageOutput="true" requestLimit="50" localOnly="true"/> 

回答

5

變化switchName="mySwitch"switchValue="Verbose"。然後通過traceource輸出所有的跡線。您可以更改switchLevel以增加/減少跟蹤的詳細程度。在你的樣本中,你追蹤了一條信息消息,有詳細的信息,警告,錯誤,關鍵。將開關設置爲警告,您將不會收到任何詳細信息或信息消息。

+0

我現在有這個相同的問題。這似乎對我沒有任何影響。 – Josh 2016-08-17 17:20:16