O'Reilly logo

ASP.NET 2.0 Cookbook, 2nd Edition by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.7. Sending Trace Data via Email with Controllable Levels

Problem

You want your application to email trace information and, at the same time, control what level of information is sent.

Solution

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.

Discussion

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required