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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
RaYell
  • 69,610
  • 20
  • 126
  • 152

2 Answers2

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>
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • Assembly name is `MyApp` and I added that to type argument – RaYell Jul 24 '09 at 09:29
  • 3
    This is giving a different message (same exception type) `Could not create MyApp.Utils.FormattedTextWriterTraceListener, MyApp. – RaYell Jul 24 '09 at 09:35
  • 3
    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 – Arsen Mkrtchyan Jul 24 '09 at 10:03
  • You are right. After modifying the constructor and adding assembly it started working. Thanks. – RaYell Jul 24 '09 at 10:23
  • I'm getting the same "Could not create.." error. What did you do to solve this?? – Pablote Mar 22 '12 at 19:43
  • @Pablote check if you put right type name and namespace in config file – Arsen Mkrtchyan Mar 23 '12 at 06:38
  • 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-constructor – HugoRune May 14 '12 at 12:10
  • Also make sure you actually have a reference to MyApp project (from the project where you define the configuration) – BornToCode Aug 12 '15 at 13:38
  • 1
    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. – user3141326 Mar 12 '16 at 19:28
  • 4
    If you got down here and you still don't have it working (as I didn't), like @user3141326 suggested, provide a constructor override for `public CustomTraceListener(string name) : base (name) { }`. The others can be omitted. – J.D. Mallen Mar 04 '19 at 20:09
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>
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
NobleGuy
  • 61
  • 1
  • 1