How to define custom TraceListener in app.config
Asked Answered
N

2

66

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="MyTextListener" 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?

Nelda answered 24/7, 2009 at 9:25 Comment(0)
D
85

Try specifying an assembly too, like so:

<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>
Digress answered 24/7, 2009 at 9:27 Comment(10)
Assembly name is MyApp and I added that to type argumentNelda
This is giving a different message (same exception type) `Could not create MyApp.Utils.FormattedTextWriterTraceListener, MyApp.Nelda
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 debugDigress
You are right. After modifying the constructor and adding assembly it started working. Thanks.Nelda
I'm getting the same "Could not create.." error. What did you do to solve this??Art
@Art check if you put right type name and namespace in config fileDigress
regarding the "Could not create..." Error: If you get that, check the InnerException of the ConfigurationErrorsException. Most likely there is an exception in the listener-constructorRomain
Also make sure you actually have a reference to MyApp project (from the project where you define the configuration)Typal
For me it was that I forgot to override a few of the base constructors. Keep in mind that in the intellisense 4 of the base constructors show up - there are two more when you press down arrows which can easily be overlooked.Clancy
If you got down here and you still don't have it working (as I didn't), like @Clancy suggested, provide a constructor override for public CustomTraceListener(string name) : base (name) { }. The others can be omitted.Christiniachristis
J
6

I've been struggling with this recently and just in case it helps anyone...

I knew my type existed so I wrote the following:

Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof("yourclassname"));
Type myClassType = assembly.GetType("yournamespace.yourclassname");

In my case, myClassType.AssemblyQualifiedName contained the string I needed in my app.config file in the type attribute.

For example:

enter image description here

<system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information,ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="CircularTraceListener" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="CircularTraceListener" type="Microsoft.Samples.ServiceModel.CircularTraceListener, CircularTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
           initializeData="C:\MyWebService\APILog\CircularTracing-service.svclog" maxFileSizeKB="1000" />
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>
Jerrold answered 22/6, 2016 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.