Recipe 13.8. 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.

To provide the ability to configure the parameters of the email sent, we have added configuration elements to the <appSettings> element in web.config, as shown below. These provide the ability to change the to address, from address, subject, and email server used to send the email, without having to recompile the code.

 <configuration> … <appSettings> <add key="EmailServer" value="mail.adelphia.net" /> <add key="CH13EmailListenerToAddress" value="msmith@smith.com" /> <add key="CH13EmailListenerFromAddress" value="appMessage@myapp.com" /> <add key="CH13EmailListererSubject" value="Application Message" /> </appSettings> … </configuration> 

See Also

Recipes 13.5 and 13.6

Example 13-22. Custom TraceListener for sending email (.vb)

 Option Explicit On Option Strict On Imports System Imports System.Diagnostics Imports System.Net.Mail Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides a trace listener that sends all trace messages ''' via email. ''' </summary> Public Class CH13EmailListenerVB Inherits TraceListener '''*********************************************************************** ''' <summary> ''' This routine writes the passed message to the event log ''' </summary> ''' ''' <param name="message">Set to the message to write</param> Public Overloads Overrides Sub Write(ByVal message As String) sendEmail(message) End Sub 'Write '''*********************************************************************** ''' <summary> ''' This routine writes the passed message to the event log ''' </summary> ''' ''' <param name="category">Set to the category for the message</param> ''' <param name="message">Set to the message to write</param> Public Overloads Overrides Sub Write(ByVal category As String, _ ByVal message As String) sendEmail(category &": " &message) End Sub 'Write '''*********************************************************************** ''' <summary> ''' This routine writes the passed message to the event log with a CR/LF ''' </summary> ''' ''' <param name="message">Set to the message to write</param> Public Overloads Overrides Sub WriteLine(ByVal message As String) sendEmail(message) End Sub 'WriteLine '''*********************************************************************** ''' <summary> ''' This routine writes the passed message to the event log with a CR/LF ''' </summary> ''' ''' <param name="category">Set to the category for the message</param> ''' <param name="message">Set to the message to write</param> Public Overloads Overrides Sub WriteLine(ByVal category As String, _ ByVal message As String) sendEmail(category &": " &message) End Sub 'WriteLine '''*********************************************************************** ''' <summary> ''' This routine sends the passed message to the email address(es) ''' defined in web.config ''' </summary> ''' ''' <param name="message"></param> Private Sub sendEmail(ByVal message As String) Dim toAddress As String Dim fromAddress As String Dim subject As String Dim client As SmtpClient Dim emailServer As String 'get the to/from addressses and subject from web.config toAddress = ConfigurationManager.AppSettings("CH13EmailListenerToAddress") fromAddress = ConfigurationManager.AppSettings("CH13EmailListenerFromAddress") subject = ConfigurationManager.AppSettings("CH13EmailListererSubject") 'send the message emailServer = ConfigurationManager.AppSettings("EmailServer") client = New SmtpClient(emailServer) client.Send(fromAddress, _ toAddress, _ subject, _ message) End Sub 'sendEmail End Class 'CH13EmailListenerVB End Namespace 

Example 13-23. Custom TraceListener for sending email (.cs)

 using System; using System.Configuration; using System.Diagnostics; using System.Net.Mail; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides a "non-web" component for demonstrating outputting /// trace information from within a class /// </summary> public class CH13EmailListenerCS : TraceListener { ///*********************************************************************** /// <summary> /// This routine writes the passed message to the event log /// </summary> /// /// <param name="message">Set to the message to write</param> public override void Write(String message) { sendEmail(message); } // Write ///*********************************************************************** /// <summary> /// This routine writes the passed message to the event log /// </summary> /// /// <param name="category">Set to the category for the message</param> /// <param name="message">Set to the message to write</param> public override void Write(String category,   String message) { sendEmail(category + ": " + message); } // Write ///*********************************************************************** /// <summary> /// This routine writes the passed message to the event log with a CR/LF /// </summary> /// /// <param name="message">Set to the message to write</param> public override void WriteLine(String message) { sendEmail(message); } // WriteLine ///*********************************************************************** /// <summary> /// This routine writes the passed message to the event log with a CR/LF /// </summary> /// /// <param name="category">Set to the category for the message</param> /// <param name="message">Set to the message to write</param> public override void WriteLine(String category,   String message) { sendEmail(category + ": " + message); } // WriteLine ///*********************************************************************** /// <summary> /// This routine sends the passed message to the email address(es) /// defined in web.config /// </summary> /// /// <param name="message"></param> private void sendEmail(String message) { String toAddress; String fromAddress; String subject; SmtpClient client; String emailServer; // get the to/from addressses and subject from web.config toAddress = ConfigurationManager. AppSettings["CH13EmailListenerToAddress"]; fromAddress = ConfigurationManager. AppSettings["CH13EmailListenerFromAddress"]; subject = ConfigurationManager. AppSettings["CH13EmailListererSubject"]; // send the message emailServer = ConfigurationManager.AppSettings["EmailServer"]; client = new SmtpClient(emailServer); client.Send(fromAddress, toAddress, subject, message); } // sendEmail } // CH13EmailListenerCS } 

Example 13-24. web.config settings for adding the trace listener and trace switch (.vb)

 <configuration> <appSettings> <add key="EmailServer" value="mail.adelphia.net" /> <add key="CH13EmailListenerToAddress" value=" msmith@smith.com" /> <add key="CH13EmailListenerFromAddress" value="appMessage@myapp.com" /> <add key="CH13EmailListererSubject" value="Application Message" /> </appSettings> … <system.diagnostics> <switches> <!-- This switch controls messages written to the event log.  To control the level of message written to the log set  the value attribute as follows:  "0" - output no messages  "1" - output only error messages  "2" - output error and warning messages  "3" - output error, warning, and informational messages  "4" - output all messages --> <add name="EventLogSwitch" value="1"/> </switches> <trace autoflush="true" indentsize="0"> <listeners> <add name="CookbookEventLogListener" type="ASPNetCookbook.VBExamples.CH13EmailListenerVB, __code" /> </listeners> </trace> </system.diagnostics> </configuration> 

Example 13-25. web.config settings for adding the trace listener and trace switch (.cs)

 <configuration> <appSettings> <add key="EmailServer" value="mail.adelphia.net" /> <add key="CH13EmailListenerToAddress" value=" msmith@smith.com" /> <add key="CH13EmailListenerFromAddress" value="appMessage@myapp.com" /> <add key="CH13EmailListererSubject" value="Application Message" /> </appSettings> … <system.diagnostics> <switches> <!-- This switch controls messages written to the event log.  To control the level of message written to the log set  the value attribute as follows:  "0" - output no messages  "1" - output only error messages  "2" - output error and warning messages  "3" - output error, warning, and informational messages  "4" - output all messages --> <add name="EventLogSwitch" value="1"/> </switches> <trace autoflush="true" indentsize="0"> <listeners> <add name="CookbookEventLogListener" type="ASPNetCookbook.CSExamples.CH13EmailListenerCS, __code" /> </listeners> </trace> </system.diagnostics> </configuration> 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net