13.7. Sending Trace Data via Email with Controllable Levels
You want your application to email trace information and, at the same time, control what level of information is sent.
Create your own trace listener that inherits from the
TraceListener class and overrides the
WriteLine methods to email their output. A sample trace listener we’ve written to demonstrate this solution is shown in Example 13-22 (VB) and Example 13-23 (C#).
In the classes you want to output trace information, create a
TraceSwitch object using the name of the
TraceSwitch you added to the web.config file, and then use the
WriteLineIf methods of the
Trace class to output the required messages, as described in Recipe 13.6.
The technique we advocate for emailing trace information involves creating your own custom trace listener. The purpose of the custom trace listener is to override the
WriteLine methods such that the information normally written to the trace sequence is emailed instead, as shown in Examples 13-22 (VB) and 13-23 (C#). When building a trace listener for this purpose, we find it useful to control the level of messages emailed, such as sending only error messages or sending error and warning messages. Controlling the level of messages that are output involves the use of switches, as described in Recipe 13.6