tags:

views:

11

answers:

1

I am learning how to use the .NET Tracing framework, and I am stuck trying to enable some fairly simple logging.

I'd like to log all messages that are at "Warning" or above to a text file, using the MyApplication.exe.config file. I would like to enable this for all sources in the application, without having to specify them all in the config file.

In my application code I am calling System.Diagnostics.PresentationTraceSources.Refresh(); and in my .config I have tried the following:

<configuration>
<system.diagnostics>
    <trace autoflush="true" indentsize="4">
        <listeners>
            <add
                name="myListener"
                type="System.Diagnostics.TextWriterTraceListener"
                initializeData="Trace.txt" />
        </listeners>
    </trace>
</system.diagnostics>
</configuration>

But this does not seem to log anything.

My only success has come from explicitly listing all the sources in the .config file; which I want to avoid as I want to catch all sources (even those I don't control).

A: 

The .NET tracing framework does not support this. It was designed from the perspective that you should only be logging to diagnose a particular problem, so you would (presumably) know which trace sources would be applicable.

If you need to capture all logging, I'd use moles to hook TraceSource construction or poll TraceSource.tracesources (using Reflection). Moles is preferable because it's cleaner, supported, and you won't lose any messages. Note that both of these approaches are not acceptable for production code; they can be used during development only.

Stephen Cleary