Jak zdefiniować własny TraceListener w aplikacji.config

Zaimplementowałem własny detektor śledzenia (pochodzący z TextWriteTraceListener) i teraz chciałbym ustawić moją aplikację, aby używała go zamiast standardowego TextWriteTraceListener.

Najpierw dodałem default TextWriteTraceListener w celu upewnienia się, że działa dobrze i działa. Oto moja aplikacja.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>

Teraz mój trace listener jest zdefiniowany w przestrzeni nazw MyApp.Utils i nazywa się FormattedTextWriterTraceListener. Więc zmieniłem typ w konfiguracji powyżej na MyApp.Utils.FormattedTextWriterTraceListener i obecnie wygląda tak:

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

Jednak teraz, gdy próbuję się zalogować coś dostaję ConfigurationErrorsException z wiadomością:

Nie można znaleźć typu dla klasy MyApp.Utils.FormattedTextWriterTraceListener.

Czy ktoś wie jak Mogę ustawić ten niestandardowy słuchacz w config i czy jest to w ogóle możliwe?

Author: abatishchev, 2009-07-24

2 answers

Spróbuj też określić asembler, w ten sposób:

<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>
 78
Author: Arsen Mkrtchyan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-03 09:03:37

Zmagam się z tym ostatnio i na wszelki wypadek, gdyby komuś to pomogło...

Wiedziałem, że mój typ istnieje więc napisałem co następuje:

Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof("yourclassname"));
Type myClassType = assembly.GetType("yournamespace.yourclassname");
W moim przypadku myClassType.AssemblyQualifiedName zawierał ciąg znaków, którego potrzebowałem w mojej aplikacji.plik konfiguracyjny w atrybucie type.

Na przykład:

Tutaj wpisz opis obrazka

<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>
 4
Author: NobleGuy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-18 06:21:00