2011-03-17 83 views
3

我將跟蹤功能添加到ASP.NET網站,因此我決定通過創建幾個原型來調查TraceSource;一個Web應用程序項目和一個網站項目。TraceSource和ASP.NET:適用於Web應用程序,而不適用於WebSites

我使用類似的Web.config每個項目登錄痕跡,Windows事件日誌:

<configuration> 
    <system.web> 
     <compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/> 
    </system.web>  
    <system.diagnostics> 
     <trace autoflush="true" /> 
     <sources> 
      <source name="HelloWorld"> 
       <listeners> 
        <add name="eventlogListener" /> 
       </listeners> 
      </source> 
     </sources>  
     <sharedListeners> 
      <add name="eventlogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="My Source" /> 
     </sharedListeners> 
    </system.diagnostics> 
</configuration> 

我只是以下基本跟蹤開始:

private static TraceSource _ts = new TraceSource("HelloWorld", SourceLevels.All); 

protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 

    _ts.TraceEvent(TraceEventType.Information, 10, "Greetings from OnLoad."); 
} 

隨着在Web應用程序項目中,我可以看到在事件日誌中創建的跟蹤。但是,通過網站項目,我不能。

是否需要網站項目使用TraceSource的附加步驟(例如:web.config設置,權限等)?

+1

額外的步驟是停止使用網站。 – 2011-03-17 18:23:55

+0

目前沒有選項。 – Bullines 2011-03-17 18:24:52

回答

0

經過一些試驗和錯誤之後,web.config中顯然需要system.codedom \ compilers節點,但我不完全確定原因。

7

其原因是因爲如果TRACE常量被定義爲編譯的一部分,則Trace方法僅由.Net編譯。在一個網站項目,要做到這一點的辦法是包括編譯器配置在web.config文件中,就像這樣:

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

注意「/ d:TRACE」編譯器選項,這使跟蹤時不變編譯你的代碼(在這個例子中是C#)。

相關問題