Home > View Post

How to use Ukadc.Diagnostics with Trace and Debug

I had an interesting comment from 'Grd' the other day asking:

"How can we make Ukadc.Diagnostics to pick up the trace written by System.Diagnostics.Trace.WriteLine?"

There are two ways to do this, you can add the listeners programmatically like so:

Trace.Listeners.Add(
    new Ukadc.Diagnostics.Listeners.ConsoleTraceListener("Message: {Message}"));

Easy peasy. Even better, you can wire the listeners in config like so:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <trace>
            <listeners>
                <add name="cons" type="Ukadc.Diagnostics.Listeners.ConsoleTraceListener, Ukadc.Diagnostics" initializeData="Message: {Message}" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

And then wherever you call Trace.WriteLine etc

Trace.WriteLine("Hello");

You can wire this up to any of the Ukadc.Diagnostics listeners (and therefore filters).

However, there is one big caveat...

When calling Trace.WriteLine and Debug.WriteLine the TraceEventCache isn't populated. I'm not sure why, but it isn't - so that means that the following tokens will fail: {DateTime}, {Callstack}, {ProcessId}, {ThreadId} and {Timestamp}. At the moment I'm considering ways to mitigate this and I've just checked in some changes that prevent the failure such that these tokens just return blanks. However, I am considering just foregoing the TraceEventCache altogether as there's no reason we can't source this information ourselves. If you think this is a good idea, be sure to vote for this feature on the Issue Tracker.

 
Josh Post By Josh Twist
7:11 AM
09 Sep 2008

» Next Post: LINQ group by query with projection
« Previous Post: BizTalk News

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.