log4net smtp logging

In umbraco v6 logging is now performed by the excellent log4net library.  In the umbraco config directory you have the log4net.config file which is where everything is setup.  The really cool thing about log4net is you can have appenders of different types e.g file, database and smtp. You can also create your own which is what I did when I wrote LiveLogger.

So I am currently working on a project and I need to do some smtp logging of errors. Something has gone very wrong and I need to know about it as its mission critical. So I added an smtp appender to the config

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="jo@site.com" />
<from value="me@site.com" />
<subject value="Info graphic error" />
<smtpHost value="localhost" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>

This works however I get too much noise. There are other errors being generated that I am not interested in. So what you can do with log4net is to an appender add a filter so you can tell it you are only interested in certain classes only


<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="MyProject.BusinessLogic.Helpers.RazorHelper" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

So now I will only get emails for errors that occur in class RazorHelper. In this class I have logging statements that look like


LogHelper.Error(typeof(RazorHelper), "Error writing to eloqua for email address " + record.EmailAddress, webex);

Advertisements

5 thoughts on “log4net smtp logging

  1. Thanks for posting this Ismail, I used to use ELMAH for reporting errors via email but since we now have better logging tools in Umbraco this proved useful to me as a log4net n00b! One step which you might want to add for others like me is that in your log4net.config file you also need to remember to add to the root node or else you won’t get any emails – caught me out!

    • It was that article I think which led me to this blog post – thanks! I believe Exceptionless is what was Exceptioneer previously? I saw recently its open sourced but I think on platforms I’m not currently familiar with and don’t have the time to invest in setting it up at the moment.

      Is it possible to extend your example to include the url of the page on which the error occurred?

      • ismailmayat says:

        Simon,

        I guess you could use HttpContext.Current.Request.ServerVariables[“HTTP_REFERER”]

        Regards

        Ismail

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s