views:

2475

answers:

1

I have implemented a custom trace listener (derived from TextWriteTraceListener) and now I would like to set my application to use it instead of standard TextWriteTraceListener.

First I added default TextWriteTraceListener in order to make sure it works ok and it does. Here's my app.config:

<configuration>
    <system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="TextListener" 
                    type="System.Diagnostics.TextWriterTraceListener"
                    initializeData="trace.log" />
            <remove name="Default" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

Now my trace listener is defined in MyApp.Utils namespace and it's called FormattedTextWriterTraceListener. So I changed the type in the config above to MyApp.Utils.FormattedTextWriterTraceListener and it currently looks like that:

<configuration>
    <system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="TextListener" 
                    type="MyApp.Utils.FormattedTextWriterTraceListener"
                    initializeData="trace.log" />
            <remove name="Default" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

However now when I try to log something I'm getting a ConfigurationErrorsException with the message: Couldn't find type for class MyApp.Utils.FormattedTextWriterTraceListener.

Does anyone knows how can I set up this custom listener in config and if it's even possible?

+6  A: 

try to specify an assembly too

<configuration>
    <system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="TextListener" 
                    type="MyApp.Utils.FormattedTextWriterTraceListener, MyApp"
                    initializeData="trace.log" />
            <remove name="Default" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>
ArsenMkrt
Assembly name is `MyApp` and I added that to type argument
RaYell
I edited post, try to specify like that
ArsenMkrt
This is giving a different message (same exception type) `Could not create MyApp.Utils.FormattedTextWriterTraceListener, MyApp.
RaYell
now he find the type but can't create an instance, may be there is an exception in the constructor or other called methods, try to set breakpoint in costructor and debug
ArsenMkrt
You are right. After modifying the constructor and adding assembly it started working. Thanks.
RaYell