TraceSource 和 ASP.NET:适用于 Web 应用程序,而不是网站

发布于 2024-10-22 21:00:48 字数 1393 浏览 5 评论 0原文

我正在向 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?

I'm adding tracing functionality to an ASP.NET website so I decided to investigate TraceSource by creating a couple of prototypes; a Web Application project and a Website project.

I'm using similar Web.config for each project to log traces to the Windows Event Log:

<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>

I'm simply starting with the following basic trace:

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.");
}

With the Web Application project, I can see the trace created in the Event Log. However, with the Website project, I cannot.

Are additional steps (Ex: web.config settings, permissions, etc) required with the Website projects to use TraceSource?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2024-10-29 21:00:48

原因是如果 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”编译器选项,该选项在编译代码时启用 TRACE 常量(本例中为 C#)例子)。

The reason for this is because the Trace methods are only compiled by .Net if the TRACE constant is defined as part of the compile. In a Web Site project, the way to do this is to include compiler configuration in your web.config file, like this:

  <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>

Note the "/d:TRACE" compiler option, which enables the TRACE constant when compiling your code (C# in this example).

时光病人 2024-10-29 21:00:48

经过一番尝试和错误后,显然 web.config 中需要 system.codedom\compilers 节点,但我不完全确定原因。

After some trial and error, apparently the system.codedom\compilers node is required in the web.config, but I'm not entirely sure why.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文