I’m trying to set up .NET tracing. I’m able to get basic tracing to work via System.Diagnostics.Trace, but for complicated reasons I have to activate tracing via System.Diagnostics.TraceSource objects (the new way of doing it, since .NET 2.0) rather than using System.Diagnostics.Trace. I've tried everything but it just doesn't want to work using TraceSource. I am performing the tracing in an ASP.NET code-behind (aspx.cs)
Here are some related URLs:
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 htt开发者_JAVA技巧p://msdn.microsoft.com/en-us/library/b0ectfxd%28v=VS.100%29.aspxCurrently, based on what is in web.config, it should be tracing both to a file and to the page, from this code:
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();
Here’s the web.config sections that are relevant:
<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"/>
change switchName="mySwitch"
to switchValue="Verbose"
. This then outputs ALL traces via tracesource. You can change the switchLevel
to increase / reduce the verbosity of the tracing. In your sample you have traced an information message, there is, verbose, information, warnings, error, critical. Set switch to warning and you won't get any verbose or information messages.
精彩评论