gpt4 book ai didi

c# - TraceSource 和 ASP.NET : Works for Web Applications, 不是网站

转载 作者:行者123 更新时间:2023-11-30 14:17:53 27 4
gpt4 key购买 nike

我正在向 ASP.NET 网站添加跟踪功能,因此我决定调查 TraceSource通过创建几个原型(prototype);一个 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?

最佳答案

这是因为如果 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#)时启用 TRACE 常量。

关于c# - TraceSource 和 ASP.NET : Works for Web Applications, 不是网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5342210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com