Email exception stack using Logging Application Block: Email Trace Listener

In application trouble shooting and bug fixing exception stack provides relevant information to handle the exception in efficient manner.

Prerequisites

Note that in order to implement consistent exception handling throughout the application Enterprise Library: Exception Handling Block configuration and logging the exceptions to windows events are discussed in the article Enterprise Library:Exception Handling Application BlockIf it is the first time you are using application blocks from Enterprise Library I would suggest to go through this article first before configuring email trace listener.

Note that this article assumes that you have already added required Enterprise Library application blocks DLLs references to your web application, which is explained here.

Introduction

This article discusses configuring Email Trace Listener from Enterprise Library: Logging Application Block to email exception stack to the configured email address. Email trace listener writes an email message, formatting the output with an ILogFormatter.

When there is an exception with an application relevant person must be notified. One of the best ways is to send email to a distribution mailbox using the email trace listener.

1. Configuring Email Trace Listener

Create an Email Trace Listener from Trace listeners section by selecting context menu. Enterprise Library configuration tool opens Email trace listener to configure as shown below.

ToAddress, FromAddress, SmtpServer and SmtpPort properties allow to configure the EmailTraceListener class.

In order to send an email it is required to know SmtpServer and SmtpPort. These properties initially default to localhost IP address and 25 respectively. Make sure to configure mandatory properties SmtpServer, SmtpPort, ToAddress and ToAddress. Note that SubjectLinerHeader and SubjectLineEnder are optional properties and they represent email subject line suffix and prefix.

Email_Trace_Listener_Config

Picture 1:- Email Trace Listener configuration

2. Add Email Trace Listener to Category Sources

Create new Trace Listener reference under desired category source by selecting the context menu from the Category Source and choose Email TraceListener as referenced Trace Listener.

In below configuration Email Trance Listener is the referenced trace listener for category source Error. When an error occurs with in the application an email is sent to configured address using Email trace listener.

 

image

Note that using Enterprise Library application blocks configuration tool updates Web.config

Email trace listener configuring looks similar to the one below.

 

   1: <listeners>
   2:             <add toAddress="xyz@xyz.com" 
   3:                 fromAddress="xyz@xyz.com" 
   4:                 subjectLineStarter="" 
   5:                 subjectLineEnder=":" 
   6:                 smtpServer="SMTP Server" 
   7:                 smtpPort="SMTP Port No" 
   8:                 formatter="Text Formatter" 
   9:                 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.
  10:                 Configuration.EmailTraceListenerData, Microsoft.Practices.
  11:                 EnterpriseLibrary.Logging, 
  12:                 Version=4.1.0.0, 
  13:                 Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
  14:                 traceOutputOptions="None" 
  15:                 filter="All" 
  16:                 type="Microsoft.Practices.EnterpriseLibrary.Logging.
  17:                 TraceListeners.EmailTraceListener, 
  18:                 Microsoft.Practices.EnterpriseLibrary.Logging, 
  19:                 Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
  20:                 name="Email TraceListener"/>
  21: </listeners>

Category Sources configuration looks similar to the one below

 

   1: <categorySources>
   2:             <add switchValue="All" name="General">
   3:                 <listeners>
   4:                     <add name="Email TraceListener"/>
   5:                 </listeners>
   6: </categorySources>

 

Conclusion:- That is it. Enterprise Library: Logging application block Email trace listener configuration is done. When the exception is caught your web application will send an email with exception stack.

Testing

In order to test this copy the below code snippet in Page_Load or your desired event, making sure that you are using correct exception policy configured. Note that exception policy configuration is discussed in step by step article Exception Handling Application Block with Logging Application Block. You should receive an email message with exception stack just in few seconds.

clip_image022

 

References:-

Hands-On Labs are best resources to experiment further. Note that these labs consists of step by step tutorial for all Enterprise Library Application Blocks. Choose the Logging Application Block, which is relevant to this article.

1 Comment

Comments have been disabled for this content.