【问题标题】:.NET tracing not working with Diagnostics.TraceSource, only Diagnostics.Trace.NET 跟踪不适用于 Diagnostics.TraceSource,仅适用于 Diagnostics.Trace
【发布时间】:2010-09-29 15:07:35
【问题描述】:

我正在尝试设置 .NET 跟踪。我可以通过 System.Diagnostics.Trace 进行基本跟踪,但由于复杂的原因,我必须通过 System.Diagnostics.TraceSource 对象(从 .NET 2.0 开始的新方法)而不是使用 System 来激活跟踪.诊断.跟踪。我已经尝试了所有方法,但它只是不想使用 TraceSource。我在 ASP.NET 代码隐藏 (aspx.cs) 中执行跟踪

以下是一些相关的网址:

http://msdn.microsoft.com/en-us/library/ty48b824.aspx
http://msdn.microsoft.com/en-us/library/64yxa344.aspx
http://msdn.microsoft.com/en-us/library/sk36c28t.aspx
http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396431.aspx
http://msdn.microsoft.com/en-us/library/b0ectfxd%28v=VS.100%29.aspx

目前,根据 web.config 中的内容,它应该从以下代码跟踪文件和页面:

TraceSource ts = new TraceSource("mysource", SourceLevels.All);
Trace.Write("Trace (old way)"); // this one works
ts.TraceInformation("Trace (new way)"); // this one doesn't work
ts.Flush();
ts.Close();

以下是相关的 web.config 部分:

 <system.diagnostics>
       <trace autoflush="false">
            <listeners> <!-- these listeners activate the "old way" of tracing. -->
                 <add       name="pagelistener" />
                 <add       name="filelistener" />
            </listeners>
       </trace>

       <sources>
            <source name="mysource" switchName="myswitch">
                 <listeners>  <!-- these listeners activate the "new way" -->
                       <add name="pagelistener" />
                       <add name="filelistener" />
                 </listeners>
            </source>
       </sources>


       <sharedListeners> 
            <!-- these are the actual trace listeners -->
            <add
                  name="filelistener"
                 type="System.Diagnostics.TextWriterTraceListener"
                 initializeData="loplog.txt"
                 />
            <add
                 name="pagelistener"
                 traceOutputOptions="none"
                 type="System.Web.WebPageTraceListener, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                 />
       </sharedListeners>

       <switches>
            <!-- the sources above use this verbose switch -->
            <add name="myswitch" value="Verbose"/>
       </switches>

 </system.diagnostics>
 <system.codedom>
       <!-- this compilers section should not be needed because I added
                 #define TRACE to the .aspx.cs file, however I put this in
                 since it's still not working. -->

       <compilers>
            <compiler
                            language="c#;cs;csharp"
                            extension=".cs"
                            compilerOptions="/d:TRACE"
                            type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                            warningLevel="1"
                            />
       </compilers>
 </system.codedom>

 <system.web>
       <!-- this trace tag should be redundant because I added trace="true" to the aspx file,
                 but I put it in here anyway because this wasn't working. -->
       <trace writeToDiagnosticsTrace="true" enabled="true" pageOutput="true" requestLimit="50" localOnly="true"/>

【问题讨论】:

  • 在我的 ASP.NET Web 服务 (ASMX) 中,我遇到了完全相同的问题。就我而言,我在我的 ASMX 代码隐藏的顶部添加了#define TRACE,然后它就起作用了。我看到您也在尝试定义 TRACE 标志,所以我不确定发生了什么。

标签: c# .net asp.net web-config trace


【解决方案1】:

switchName="mySwitch" 更改为switchValue="Verbose"。然后,这将通过 tracesource 输出所有跟踪。您可以更改switchLevel 以增加/减少跟踪的详细程度。在您的示例中,您跟踪了一条信息消息,有详细、信息、警告、错误、关键。将开关设置为警告,您将不会收到任何冗长或信息性消息。

【讨论】:

  • 我现在也遇到了同样的问题。这似乎对我没有任何影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多