TraceSource from referenced library not logging when programmatically added

1k Views Asked by At

Here is my config file from my Winforms application. Currently it has MyTraceSource commented out, and if I uncomment it and run my program it works as expected. I get output to my console from my referenced library. MyTraceSource is a Trace source that is instantiated and used in my referenced library that I'll call MyOposServiceObject. You'll notice that I setup another TraceSource in my Logger.cs file called TestApplication. That trace source is my logging that I use in the test application (what a logical name...) Just so that I'm clear there are 2 trace sources instantiated and used in 2 different projects. One is a class library, and the other is a winforms application. If i compile my winforms application as a console program and uncomment my TraceSource in the app.config file I get logging from MyOposServiceObject to the console. Clear as mud??? ok on to some code.

App.config

<system.diagnostics>
    <trace autoflush="true"/>
    <sharedListeners>
        <!-- Outputs to a Log File-->
        <add name ="file" type ="System.Diagnostics.TextWriterTraceListener" initializeData="DEMO.log">
            <filter type="System.Diagnostics.EventTypeFilter" initializeData="Off"/>
        </add>
        <!-- Outputs to the console-->
        <add name="console" type ="System.Diagnostics.ConsoleTraceListener" >
            <filter type="System.Diagnostics.EventTypeFilter" initializeData="All"/>
        </add>
    </sharedListeners>
    <sources>
        <!--<source name="MyTraceSource" switchValue="All" >
            <listeners>
                <remove name="Default"/>
                <add name="console"/>
            </listeners>
        </source>-->
    </sources>
</system.diagnostics>

However, I don't want my program to be a console program so I made a custom TraceListener.

MyTraceListener.cs

public class MyTraceListener : TraceListener
{
    private System.Windows.Forms.TextBox output;
    public MyTraceListener(System.Windows.Forms.TextBox output)
    {
        this.Name = "FancyTrace";
        this.output = output;
    }
    public override void Write(string message)
    {
        output.SafeSetText(string.Format("{0}\r\n[{1}] {2}",output.Text, DateTime.Now.ToString("F"), message));
    }

    public override void WriteLine(string message)
    {
        Write(message + Environment.NewLine);
    }
}

I wired it up like this

Logger.cs

    internal static void ShowDebugWindow()
    {
        if (debugWindow != null) return;

        debugWindow = new Form();
        debugWindow.TopMost = true;
        debugWindow.FormBorderStyle = FormBorderStyle.FixedToolWindow;
        TextBox tb = new TextBox();
        tb.Multiline = true;

        tb.Dock = DockStyle.Fill;
        debugWindow.Controls.Add(tb);
        MyTraceListener myTrace = new MyTraceListener(tb);
        trace.Listeners.Add(myTrace);
        opos.Listeners.Add(myTrace);
        debugWindow.Show();
    }
    private static TraceSource trace = new TraceSource("TestApplication");
    private static TraceSource opos = new TraceSource("MyTraceSource");

now trace is used in this application, and output from it indeed goes to my little debug window. but i get nothing from opos. What am I doing wrong?

1

There are 1 best solutions below

3
On

To summarize, you want all of your TraceSources from both projects to show up in both your little debug window, the console, and any shared listeners, correct?

I'm assuming your app structure is like:

- Solution
  - Class Library
    - MyTraceListener.cs
    - Logger.cs
    - OtherCodeThatUsesLogger.cs
  - Winforms Project : References Class Library
    - app.config
    - WinformsCodeThatUsesLogger.cs

If so, I think the problem here is that your consuming code isn't using the instance of the TraceSource you created in Logger.cs.

Logger.cs

public static TraceSource trace = new TraceSource("TestApplication");
public static TraceSource opos = new TraceSource("MyTraceSource");

Consuming code

void foo() {
    Logger.trace.TraceInformation("hello... ");
    Logger.opos.TraceInformation("muggles.");
}