How to define custom TraceListener in app.config

风流意气都作罢 提交于 2019-11-26 22:33:20

问题


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?


回答1:


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>



回答2:


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:

<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>


来源:https://stackoverflow.com/questions/1176582/how-to-define-custom-tracelistener-in-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!