TraceSource 和 ASP.NET:适用于 Web 应用程序,而不是网站
我正在向 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因是如果 TRACE 常量被定义为编译的一部分,则 Trace 方法仅由 .Net 编译。在网站项目中,执行此操作的方法是在 web.config 文件中包含编译器配置,如下所示:
注意“/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:
Note the "/d:TRACE" compiler option, which enables the TRACE constant when compiling your code (C# in this example).
经过一番尝试和错误后,显然 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.