2011-03-17 82 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#)。

相关问题