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 Write and 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#).

Next, modify your web.config file to add the custom TraceListener and TraceSwitch, as shown in Example 13-24 (VB) and Example 13-25 (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 WriteIf and 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 Write and 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

Get ASP.NET 2.0 Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.