Log4Net email logging

Log4Net is the great logging tool used by Umbraco for its logging. Others such as Darren Ferguson and Ismail Mayat give great introductions, but this short piece is just to show how I congigured Log4Net to send an email when an error occured in a particular class.

The following config sample in the xml > log4net section of /Config/log4net.config sends an email to a configurable email address when an error is thrown in a configurable class:

  <root>
    <priority value="Error"/>
    <appender-ref ref="APPENDER_NAME" />
  </root>  
  

  <appender name="APPENDER_NAME" type="log4net.Appender.SmtpAppender">
    <to value="EMAIL_TO" />
    <from value="EMAIL_FROM" />
    <subject value="EMAIL_SUBJECT" />
   
    <smtpHost value="SMTP_HOST_NAME" />
    <port value="SMTP_PORT" />
    <authentication value="Basic"/>
    <userName value="SMTP_USERNAME" />
    <password value="SMTP_PASSWORD" />
    <enableSsl value="SMTP_ENABLE_SSL" />
   
    <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>

    <filter type="log4net.Filter.LoggerMatchFilter">
      <loggerToMatch value="LOGGER_TO_MATCH" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    
  </appender>

APPENDER_NAME: can be anything you like, e.g. MySmtpAppender
EMAIL_TO: any valid email address, e.g. recipient@domain.com
EMAIL_FROM: any valid email address, e.g. no-reply@domain.com
EMAIL_SUBJECT: can be anything you like, e.g. My email subjact
SMTP_HOST_NAME: SMTP server used to send the email, e.g. email-smtp.eu-west-1.amazonaws.com
SMTP_PORT: SMTP port number, typically 25, 26, 465, or 587
SMTP_USERNAME: username used to login to the SMTP server
SMTP_PASSWORD: password used to login to the SMTP server
SMTP_ENABLE_SSL: SMTYP connection using SSL encryption, e.g. true or false
LOGGER_TO_MATCH: fire log event when event occurs in this fully qualified class name, e.g. My.Namespace.MyClass

Refeneces