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

2

6

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>
Eellike answered 18/8, 2012 at 23:7 Comment(0)
D
3

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>
Desmoid answered 4/10, 2012 at 11:55 Comment(0)
C
0

Use TraceSource.

  1. Initialize it in your trace source.

    TraceSource logger = new TraceSource("Class1");

  2. Call it from critical points in code:

    logger.TraceInformation("Hello from Class1");

  3. Be sure to edit your application configuration:

<system.diagnostics> <trace autoflush="true"/> <sources> <source name="Class1" switchName="Class1Switch" switchType="System.Diagnostics.SourceSwitch"> <listeners> <add name="console"></add> <add name="csv" /> <!-- or you can add your own listener here --> </listeners> </source> </sources> <switches>
<add name="Class1Switch" value="Information" />
</switches> <sharedListeners> <add name="console" type="System.Diagnostics.ConsoleTraceListener" /> <add name="csv" type="System.Diagnostics.DelimitedListTraceListener" delimiter="|" initializeData="d:\data\tracing\trace.log" traceOutputOptions="Timestamp, ThreadId, LogicalOperationStack, DateTime, ProcessId"> </add> </sharedListeners> </system.diagnostics>

If say, you want to only log errors, change the switch:

<add name="Class1Switch" value="Error" />

To switch it completely off:

<add name="Class1Switch" value="Off" />
Croom answered 11/2, 2016 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.