Email exceptions

Topics: Building and extending application blocks, Enterprise Library Core, Exception Handling Application Block
May 29, 2008 at 5:18 AM
Edited May 30, 2008 at 4:17 AM

Hi
How do I add email support for exception handling?  I used to use the old version of the Enterprise library where I would create a publisher object to publish the email.  I don't see the publisher interface anymore in the Enterprise Application Block V4.  I must be missing something simple since I see a lot of posts where people say they are emailing exceptions but no one posts the code.

Thanks,
Vince

Jul 7, 2008 at 10:41 AM
Hi Vince,
I am a novice to EntLib. I did as follows: [Hope this might help]
1. Created a Class Lib containing a custom handler: SendMailExceptionHandler
[

using

System;

 

using

System.Collections.Specialized;

 

using

System.Text;

 

using

System.Net.Mail;

 

using

Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

 

using

Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;

 

using

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;

 

using

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

 

namespace

CustomHandlerSendExceptionMail

 

{

 

/// <summary>

 

 

/// MS Enterprise Library Training Assignment

 

 

/// *******************************************************************

 

 

/// Developer: Tarique Iqbal, Programmer, SSD, LEADSoft Bangladesh Ltd

 

 

/// Start Date: June 18, 2008 End Date:

 

 

/// Last Modified:

 

 

/// *******************************************************************

 

 

///

 

 

/// This Class Extends IExceptionHandler to extend EHAB

 

 

///

 

 

/// </summary>

 

[

ConfigurationElementType(typeof(CustomHandlerData))]

 

 

public class SendExceptionMailHandler : IExceptionHandler

 

{

 

public SendExceptionMailHandler(NameValueCollection ignore)

 

{

}

 

public Exception HandleException(Exception exception, Guid correlationID)

 

{

SendExceptionAsEmailToAdministrator(exception);

 

return exception;

 

}

 

private void SendExceptionAsEmailToAdministrator(Exception exception)

 

{

 

try

 

{

 

MailMessage message = new MailMessage("EXCEPTION@noreply.com", "xyz@leads-xx.com", "EHAB (email) Testing: Tarique", exception.ToString());

 

message.CC.Add(

new MailAddress("x@leads-xx.com"));

 

message.CC.Add(

new MailAddress("x@leads-xx.com"));

 

 

SmtpClient emailClient = new SmtpClient("192.168.x.x");

 

System.Net.

NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("x@leads-xx.com", "xxxxxxxx");

 

emailClient.UseDefaultCredentials =

false;

 

emailClient.Credentials = SMTPUserInfo;

emailClient.Send(message);

}

 

catch (Exception ex)

 

{

 

throw ex;

 

}

}

}

}

]

2.Then Added the ref to Web Project
3. Then changed the config
<exceptionHandling>

 

<

exceptionPolicies>

 

<

add name="Email Exception Policy">

 

<

exceptionTypes>

 

<

add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

 

 

 

 

postHandlingAction="None" name="Exception">

 

<

exceptionHandlers>

 

<

add type="CustomHandlerSendExceptionMail.SendExceptionMailHandler, CustomHandlerSendExceptionMail"

 

 

 

 

name="Send Email Handler" />

 

</

exceptionHandlers>

 

</

add>

 

</

exceptionTypes>

 

</

add>

 

</

exceptionPolicies>

 

</

exceptionHandling>

 


4. The UI Code

 

protected void btnException_Click(object sender, EventArgs e)

 

{

lblMesg.Text =

"";

 

 

try

 

{

GenerateException();

}

 

catch (Exception ex)

 

{

 

bool rethrow = ExceptionPolicy.HandleException(ex, "Email Exception Policy");

 

 

if (rethrow)

 

{

 

// Something has gone very wrong

 

lblMesg.Text =

"Sending Exception Data via email failed.";

 

}

 

else

 

lblMesg.Text =

"Exception data sent to administrator.";

 

}

}

 

private void GenerateException()

 

{

 

throw new Exception("Training Home work testing: Critical Exception occured");

 

}