Pregunta

Estoy usando el método siguiente para agregar un registro de rastreo:

TraceSource.TraceEvent(TraceEventType, Int32, String)

donde Int32 representa Identificación del evento.

Entonces, ¿cómo filtro en TraceSwitch para escuchar solamente por identificación del acontecimiento específico? Ir esto es imposible?

<system.diagnostics>
    <sources>
        <source name="MyTraceSource" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch>"
            <listeners>
                <add name="console" type="System.Diagnostics.ConsoleTraceListener" />
            </listeners>
        </source>
    </sources>
    <switches>
        <add name="sourceSwitch" value="?" />
    </switches>
</system.diagnostics>
¿Fue útil?

Solución

It's possible but you need to write a custom TraceFilter and override the ShouldTrace method. The id is passed to it, but no out-of-the-box filter supports it.

Then, you can declare it like this in a .config file:

<source name="MyTraceSource" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
  <listeners>
    <add name="console" type="System.Diagnostics.ConsoleTraceListener">
      <filter type="YourNamespace.YourFilter, YourAssembly, ..." />
    </add>
  </listeners>
</source>

Otros consejos

You can try Ukadc.Diagnostics from codeplex. This project provides some useful extensions for System.Diagnostics. The coolest thing, in my opinion, that they provide is a token based system that can be used to define log/trace output format similar to what you can achieve with log4net and NLog. It is a configuration-only dependency. That is, if you code is already using TraceSources, you only have to put Ukadc.Diagnostics on your machine and have your app.config point to their TraceListeners, PropertyTokens, etc.

You still instrument your code using System.Diagnostics.TraceSource objects.

To your point, using Ukadc.Diagnostics you can filter based on most property tokens (including EventId).

Note that the token system can only be used (as far as I know) with the corresponding TraceListeners provided in Ukadc.Diagnostics (or any TraceListener that you write based on their base TraceListener class).

I have not used this project in production, but I have fooled around with it quite a bit and have been quite impressed. It works well and is easy to extend.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top