Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I turn off tracing for a single assembly that my code references?

I have a certain framework of code, and I have a TraceListener defined for two reasons:

  • Back-compatibility with a lot of the old logging that was done via Trace.Write until we update it, and
  • It's nice to be able to instrument the other assemblies our code references if we need to.

However, I have one assembly (not ours) that logs a lot of pointless data that doesn't help us debug anything. How can I turn off tracing for this one assembly (or, alternately, the facade project we built around it), while leaving it on for the rest of the application?

I've tried various flavors of configuration in our facade project, usually looking like the following, to no avail. I've tried adding <remove> elements that match the <add> elements which setup the logging in the first place, tried <clear>ing them, setting <trace enabled="false"> and at least three other attempts. Thanks for any help you can provide!

<system.diagnostics>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <clear/>
      </listeners>
    </trace>
    <switches>
    </switches>
  </system.diagnostics>
like image 300
RJ Cantrell Avatar asked Aug 18 '12 23:08

RJ Cantrell


People also ask

How do I turn off tracing in WCF?

If you want to disable the trace source, you should use the logMessagesAtServiceLevel , logMalformedMessages , and logMessagesAtTransportLevel attributes of the messageLogging element instead. You should set all these attributes to false.

What is system diagnostics trace?

Tracing helps you isolate problems and fix them without disturbing a running system. This class provides methods to display an Assert dialog box, and to emit an assertion that will always Fail. This class provides write methods in the following variations: Write. WriteLine.

What is tracing in C#?

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. We can access and manipulate trace messages programmatically.

What is tracing in WCF?

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.


1 Answers

You can write your own trace filter, for use with your TraceListener. Inside this filter you can look for your assembly in stackTrace and turn off event tracing.

In my case I wrote filter (see: DotNetOpenAuthFilter) based on EventTypeFilter, which filters events only from the DotNetOpenAuth library.

Then connect the filter to the listener in the web.config:

<configuration>
  <system.diagnostics>
    <trace>
      <listeners>
        <add name="console" type="System.Diagnostics.ConsoleTraceListener" >
          <filter type="Common.Log.DotNetOpenAuthFilter, Common" initializeData="Warning" />
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>
like image 130
Michal Avatar answered Sep 28 '22 02:09

Michal