custom TraceListener accessible via code but not via app.config

M

michael sorens

I created a custom TraceListener by subclassing a TextWriterTraceListener.
(TextWriterTraceListener has most of what I want already.) My custom
TraceListener works fine when I instantiate it through code. When I attempt
to instantiate it in the app.config file, however, it is not able to find the
class. In the same config file I reference several other standard
TraceListeners that work just fine. Here is the relevant portion of the
app.config file:

<add name="MyListener"
type="TraceSourceGuiApp.MyTextWriterTraceListener"
initializeData="diagnostic.txt">
<filter type="System.Diagnostics.EventTypeFilter"
initializeData="Off"/>
</add>

The exact error is:

ConfigurationErrorsException - Couldn't find type class for
TraceSourceGuiApp.MyTextWriterTraceListener

The error manifests when I attempt to access a different listener in the
Listener collection.

Here is the alternate code version that works just fine:

MyTextWriterTraceListener l =
new MyTextWriterTraceListener("diagnostics.txt", "MyListener");
mySource.Listeners.Add(l);

I initially set this up so the custom listener class is in a separate DLL
but I also tried it within the same project and got the same result.

What is the appropriate way to access a custom listener from app.config?

[Env: .Net 2.0]
 
S

Steven Cheng

Hi Michael,

As for the custom TraceListener assembly not found issue, I think it is
likely due to the configuration setting you used.

For any .NET configuration element that will require you supply a class
type, it will need both Type name and Assembly name, e.g.

<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener" type="MyNamespace.MyTraceListener,
MyAssemblyName" ..... />
<remove name="Default" />
</listeners>

I think you may have followed the example in the following reference:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.tracelistener.as
px

the MSDN example use a built-in trace Listener that is in built-in core
assembly, so the runtime will be able to locate it (even if you do not
explicitly supply assembly name). However, for your own custom trace
listener, you need to supply a Full type name with assembly name. Also, if
the assembly is strong-named, you need to supply the asembly name with all
the four parts (assembly name, version, culture, public key token).

In addition, if you have interests, you can use the fuslogvw.exe tool to
trace how the runtime is locating the certain assembly and why it fails:

#Assembly Binding Log Viewer (Fuslogvw.exe)
http://msdn2.microsoft.com/en-us/library/e74a18c4(vs.71).aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.





--------------------
From: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= <[email protected]>
Subject: custom TraceListener accessible via code but not via app.config
Date: Sun, 17 Feb 2008 22:31:02 -0800
I created a custom TraceListener by subclassing a TextWriterTraceListener.
(TextWriterTraceListener has most of what I want already.) My custom
TraceListener works fine when I instantiate it through code. When I attempt
to instantiate it in the app.config file, however, it is not able to find the
class. In the same config file I reference several other standard
TraceListeners that work just fine. Here is the relevant portion of the
app.config file:

<add name="MyListener"
type="TraceSourceGuiApp.MyTextWriterTraceListener"
initializeData="diagnostic.txt">
<filter type="System.Diagnostics.EventTypeFilter"
initializeData="Off"/>
</add>

The exact error is:

ConfigurationErrorsException - Couldn't find type class for
TraceSourceGuiApp.MyTextWriterTraceListener

The error manifests when I attempt to access a different listener in the
Listener collection.

Here is the alternate code version that works just fine:

MyTextWriterTraceListener l =
new MyTextWriterTraceListener("diagnostics.txt", "MyListener");
mySource.Listeners.Add(l);

I initially set this up so the custom listener class is in a separate DLL
but I also tried it within the same project and got the same result.

What is the appropriate way to access a custom listener from app.config?

[Env: .Net 2.0]
 
M

michael sorens

You pinpointed just what I needed; adding the assembly name was what I
needed. Thanks!

For my own edification, I attempted to view the error with the Assembly
Binding Log Viewer as you suggested, but it listed no errors, even after I
cleared the IE cache as the reference page suggested as a troubleshooting
measure. I note, however, that the reference seems to indicate that it will
only notice errors precipitating from a TypeLoadException whereas my issue
manifested as a ConfigurationErrorsException; perhaps the TraceSource masks
the TypeLoadException internally...?
 
S

Steven Cheng

Thanks for your reply Michael,

I haven't got deep into the configuration class component, however, the
problem here does be caused by assembly loading(Type loading exception is
also due to the assembly not finding). Yes, framework class may catch and
encapsulate some raw exception so as to provde a more user friendly one.
Anyway, glad that you've got it working.

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top