Filter Diagnostic Trace Using Configuration

I often have situations when I want to have different levels of diagnostic trace in different environments, and want to configure this in web.config or app.config. Here is an example of how to do it. This example traces to the eventlog with information, warning and error levels.

  <system.diagnostics>
    <trace autoflush="true" indentsize="0">
      <listeners>
        <add initializeData="MyEventSource" type="System.Diagnostics.EventLogTraceListener" name="MyEventListener">
          <filter type="System.Diagnostics.EventTypeFilter" initializeData="Information" />
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
Advertisements