Page 1 of 1

E-mail notification when errors occur

Posted: Fri Jul 17, 2009 6:43 am
by NC Software
It would have really helped me, the System Admin (whatever the role is called) to get an e-mail notification of error log entries. If I had been getting notified by e-mail, SMS, whatever, that an error occurred, I would have been on top of the e-mail template system as it generated errors when trying to parse the nVelocity stuff that got pooched by the FCK'ing editor. I suggest add an option to e-mail the admin (or enter an address for error notifications). I just now noticed there were errors in the error log and they indicated the e-mail templates were pooched but I wasn't aware of the errors.

Re: E-mail notification when errors occur

Posted: Wed Jul 22, 2009 9:51 am
by mazhar
Read following thread
viewtopic.php?f=42&t=11796

Re: E-mail notification when errors occur

Posted: Wed Jul 22, 2009 10:35 am
by AbleMods
That NEEDS to go into 7.0.5

Re: E-mail notification when errors occur

Posted: Wed Jul 22, 2009 11:11 am
by calvis
Thanks Mazhar,

I have added that to my code snippet library.

Re: E-mail notification when errors occur

Posted: Wed Jul 22, 2009 2:36 pm
by jmestep
Yes, that is great. I've had code in a global.asax file and code that runs with Elmah.dll stuff, but neither of them works well consistently.

Re: E-mail notification when errors occur

Posted: Wed Jul 22, 2009 4:18 pm
by Logan Rhodehamel
Another way to approach this would be to alter the log4net.config file. I should construct a tutorial on that.

Re: E-mail notification when errors occur

Posted: Fri Jul 24, 2009 10:28 am
by Logan Rhodehamel
As I mentioned in my previous post, we use log4net as our logging facility. This is a fairly common logging system that is used by a wide variety of applications. It is also highly configurable.

All of the settings for log4net reside in App_Data\log4net.config. NOTE: Any change to log4net.config will require a manual restart of the application to take effect. The quick hack way of doing this is to open the web.config in the root of your website, go to the end of the file, press enter, and save. The file change will trigger an automatic restart of the app and reload the log4net.config file.

The log4net library supports a huge range of "appenders"... It is the appender that decides whether to log your error to file, to database, to the console, to the windows event log, to email, etc. You can configure multiple appenders.

One benefit to doing this for the email vs a global.asax change is that you can also get context information... log4net will send you X number of messages prior to the exception. It may or may not be helpful. I am going to post a sample log4net.config file that has been modified to send email:

Code: Select all

<log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="App_Data\\Logs\\app.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>
  <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="to@domain" />
    <from value="from@domain" />
    <subject value="Subject of error message" />
    <smtpHost value="smtp.server.address" />
    <bufferSize value="10" />
    <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>
  <root>
    <level value="All" />
    <appender-ref ref="RollingLogFileAppender" />
    <appender-ref ref="SmtpAppender" />
  </root>
</log4net>
In the SmtpAppender definition, buffer size indicates how many of the prior log messages will be included. Threshold indicates what level will register the email. I have it set to WARN. You could also set it to ERROR to only see more critical problems.

Re: E-mail notification when errors occur

Posted: Fri Jul 24, 2009 10:30 am
by Logan Rhodehamel
I have moved this thread to the main forum as others may be interested to see how to make this configuration change.