3

我正在向 ASP.NET 网站添加跟踪功能,因此我决定通过创建几个原型来研究TraceSource ;一个 Web 应用程序项目和一个网站项目。

我为每个项目使用类似的 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 应用程序项目,我可以看到在事件日志中创建的跟踪。但是,对于网站项目,我不能。

网站项目是否需要其他步骤(例如:web.config 设置、权限等)才能使用 TraceSource?

4

2 回答 2

7

这样做的原因是,如果将 TRACE 常量定义为编译的一部分,则仅由 .Net 编译 Trace 方法。在网站项目中,这样做的方法是在 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”编译器选项,它在编译代码时启用 TRACE 常量(本例中为 C#)。

于 2011-05-10T16:43:49.220 回答
0

经过反复试验,web.config 中显然需要 system.codedom\compilers 节点,但我不完全确定原因。

于 2011-03-17T19:48:30.420 回答