.NET跟踪不适用于Diagnostics.TraceSource,仅适用于Diagnostics.Trace

时间:2010-09-29 15:07:36

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

我正在尝试设置.NET跟踪。我可以通过System.Diagnostics.Trace获得基本的跟踪工作,但由于复杂的原因,我必须通过System.Diagnostics.TraceSource对象(自.NET 2.0以来的新方法)激活跟踪,而不是使用System .Diagnostics.Trace。我已经尝试了一切,但它只是不想使用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"/>

1 个答案:

答案 0 :(得分:5)

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