Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send an Email When an Error Occurs

Tags:

c#

email

log4net

Currently my application is using log4net to log errors, the web.config for this is as followed:

<log4net>     <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">       <file type="log4net.Util.PatternString" value="../../logs/gateway_%date{yyyyMMdd}.log" />       <appendToFile value="true" />       <rollingStyle value="Date" />       <datePattern value="yyyyMMdd" />       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />       </layout>     </appender>     <root>       <level value="DEBUG" />       <appender-ref ref="RollingLogFileAppenderOutput" />     </root>   </log4net> 

However, the client now wants each error to be emailled to them.

What is the easiest way to do this, can you do it within the web.config file?

like image 518
swade1987 Avatar asked Apr 30 '12 13:04

swade1987


People also ask

How come when I try to send an email it says error?

Often, this error message has the same cause as the Mailbox not found message. However, occasionally there's a problem with the recipient's email account that will resolve itself over time. Try this fix: Wait a short time and try to send your message again. If that fails, check the recipient's email address for typos.

How do I automatically flag emails in Outlook?

On the Message tab, in the Tags group, select Follow Up, and then select Add Reminder. Select Flag for Recipients. To send a flag without a reminder alert, clear the Reminder check box. Select the Flag for Me check box, and if you want, the Reminder check box.

How do you bounce back an email in Outlook?

In the web version of Outlook, click the gear icon, then Automatic Replies. If you don't see the Automatic Replies button, you might need some extra help to get started. Go to aka.ms/autoreply to learn more. In the Automatic Replies window, select Send automatic replies.

How do I check email errors?

How does the error checker work? When you've finished editing your email, click the Check & Preview button and select “Check for errors” from the drop-down menu. Click on an error from the list to resolve it. Errors are listed in order of where they appear in your email template from top to bottom.


1 Answers

You should use SmtpAppender

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">     <to value="[email protected]" />     <from value="[email protected]" />     <subject value="test logging message" />     <smtpHost value="SMTPServer.domain.com" />     <bufferSize value="512" />     <lossy value="true" />     <evaluator type="log4net.Core.LevelEvaluator">         <threshold value="WARN"/>     </evaluator>     <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />     </layout> </appender>  <logger name="ErrorLogger">     <level value="Error" />     <appender-ref ref="FileAppender" /> </logger> <logger name="EmailLogger">     <level value="Error" />     <appender-ref ref="SmtpAppender" /> </logger> 

In order to send emails only for an specific error you could do something like this

try {   // your logic } catch (MySpecificException ex) {    // I only send emails for exception of type MySpecificException     LogManager.GetLogger("EmailLogger").Error(ex);  } catch (Exception ex) {    // Just log to a file for the rest    LogManager.GetLogger("ErrorLogger").Error(ex);  } 
like image 92
Claudio Redi Avatar answered Sep 17 '22 23:09

Claudio Redi