Daniel Cazzulino's Blog : How to diagnose Linq to SQL easily and production-ready

Subscriptions

News

Source code published in this blog is public domain unless otherwise specified.

 

kzu in LinkedIn

  Microsoft MVP Profile

 Contact

Post Categories

How to diagnose Linq to SQL easily and production-ready

Here's what we're currently doing: we add an InitializeContext method to all DataContext-derived classes, which is called from all ctors:

    private void InitializeContext()
    {
#if DEBUG
        if (Debugger.IsAttached)
        {
            this.Log = new DebuggerTextWriter();
        }
#endif
    }

 

Note that the logger will only be set if a debugger is attached, so that it doesn't pollute the output on a console app while you're running it.

The DebuggerTextWriter is trivial too:

#if DEBUG
    internal class DebuggerTextWriter : TextWriter
    {
        public override void Write(string value)
        {
            Debugger.Log(0, "", value);
            Console.Write(value);
        }

        public override void WriteLine(string value)
        {
            Debugger.Log(0, "", value + Environment.NewLine);
            Console.WriteLine(value);
        }

        public override Encoding Encoding
        {
            get { return System.Text.Encoding.UTF8; }
        }
    }
#endif

 

In both cases I'm wrapping the code in #if DEBUG so that it doesn't even go to production code. The debugger writer will write to the console output as well as the VS debugger output window.

posted on Friday, August 22, 2008 4:08 PM by kzu

# How to diagnose Linq to SQL easily and production-ready @ Friday, August 22, 2008 4:32 PM

Here's what we're currently doing: we add an InitializeContext method to all DataContext-derived

Anonymous