tags:

views:

36

answers:

1

.NET lets you add trace statements to code. You can use

Trace.WriteLine"(Some message");

and then define a trace listener to collect those messages to a log file. OK so far.

But you can also do:

Trace.WriteLine"(Some message", "Category");

How can you set up a filter in app.config (ie .exe.config) so that only messages with a certain value of "Category" are sent to a listener. The documentation hints that you can do this but falls short of telling you how, I think! I want to define my own categories (by subsystem) and be able to route trace to various log files when needed.

(Yes I know all about log4net and how that can solve all my problems).

Nick

A: 

You will need to code an implementation of TraceFilter to filter traces by the category string. Otherwise, you could use the TraceEvent method and then use EventTypeFilter to filter traces based on the TraceEventType.

To modify filters, you use the app.config file as described in the example for the EventTypeFilter.

Jeff Yates
TraceFilter is an abstract class so gives an error. SourceFilter is accepted but I can't see how to put my category in and make it work.
NickT
By create, I mean specify your own derivation. I'll edit accordingly.
Jeff Yates
Thank you for that. I have now written my own filter class which achieves what I want. However, the underlying listener classes do not work quite correctly (and Reflector shows an inconsistency in their implementation) so I had to write a custom listener as well.I suspect that the implementation of this area is incomplete and that explains the lack of clear documentation from Microsoft.
NickT