Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom TraceListener in app.config

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?

like image 244
RaYell Avatar asked Jul 24 '09 09:07

RaYell


People also ask

How do I add a trace listener to an application?

We recommend the use of application configuration files, because they let you add, modify, or remove trace listeners without having to change your code. Declare your trace listener in your application configuration file. If the listener you are creating requires any other objects, declare them as well.

How many listeners can be defined in a trace listener collection?

You can define unlimited number of listeners, and as soon as you add them to trace listener collection, they will start receiving trace messages. . NET Framework comes with three ready-made listeners: DefaultTraceListener, EventLogTraceListener and TextWriterTraceListener (all in System.Diagnostics namespace).

What is the default trace listener in Java?

The System.Diagnostics.Debug and System.Diagnostics.Trace classes send messages to objects called listeners that receive and process these messages. One such listener, the System.Diagnostics.DefaultTraceListener, is automatically created and initialized when tracing or debugging is enabled.

What if I don't want my listener to receive trace output?

If you do not want your listener to receive trace output, do not add it to the Listeners collection. You can emit output through a listener independent of the Listeners collection by calling the listener's own output methods. The following example shows how to write a line to a listener that is not in the Listeners collection.


2 Answers

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> 
like image 112
Arsen Mkrtchyan Avatar answered Oct 09 '22 04:10

Arsen Mkrtchyan


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> 
like image 42
NobleGuy Avatar answered Oct 09 '22 03:10

NobleGuy