How to define custom TraceListener in app.config

前端 未结 2 2045
时光取名叫无心
时光取名叫无心 2020-12-04 10:55

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

相关标签:
2条回答
  • 2020-12-04 11:34

    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>
    
    0 讨论(0)
  • 2020-12-04 11:50

    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>
    
    0 讨论(0)
提交回复
热议问题