Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET tracing not working with Diagnostics.TraceSource, only Diagnostics.Trace

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
http://msdn.microsoft.com/en-us/library/b0ectfxd%28v=VS.100%29.aspx

Currently, 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"/>
like image 455
eeeeaaii Avatar asked Sep 29 '10 15:09

eeeeaaii


People also ask

How do I check my server's diagnostic trace logs?

Right-click a server. To view the trace log file, select Open Log Files > Trace File from the menu. To view the messages log file, select Open Log Files > Message Log File from the menu.

How do I enable WCF tracing on client?

ServiceModel. MessageLogging trace source records all messages that flow through the system. Tracing is not enabled by default. To activate tracing, you must create a trace listener and set a trace level other than "Off" for the selected trace source in configuration; otherwise, WCF does not generate any traces.

What is system diagnostics trace?

TraceSource Class (System. Diagnostics) Provides a set of methods and properties that enable applications to trace the execution of code and associate trace messages with their source.

What is .NET Tracing?

NET 2.0 version. Tracing helps to see the information of issues at the runtime of the application. By default Tracing is disabled. Tracing has the following important features: We can see the execution path of the page and application using the debug statement.


1 Answers

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.

like image 111
Kristen Avatar answered Sep 28 '22 11:09

Kristen