Limiting Event Log Notifications in Kentico 8

Kentico 8 already provides a method for sending email notifications when an error occurs. Although useful, the settings provided in Kentico 8 do not provide as much flexibility as I wanted to handle which types of items in the event log to send out a notification for. I have created a class that can be dropped into any Kentico 8.x installation which can be configured to send out an alert email for only certain codes and types of entries.

using CMS.Base;
using CMS.EmailEngine;
using CMS.EventLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for EventLogFilteredNotifications
/// </summary>
[CustomEventLogHandler]
public partial class CMSModuleLoader
{

    /// <summary>
    /// Sends a notification for certain event log entries. It is not recommended to use this with both codesToAlert and typesToAlert empty.
    /// This will work similarly to adding an email address to the "Error notification e-mail address", except it will also send notifications
    /// of all warnings and info events. This is NOT recommended.
    ///
    /// Ideas for improving this class for use in a production environment:
    ///   * Put the codes and types into a custom setting
    ///   * Send the email from an email template instead of a hard coded string.
    /// </summary>
    private class CustomEventLogHandler : CMSLoaderAttribute
    {
        /// <summary>
        /// Only send an alert if the event log entry is one of the following codes.
        /// If this is empty, it will send an alert for all codes.
        /// </summary>
        string[] codesToAlert = {
                                    "STARTAPP",
                                    "NOT_FOUND"
                                };

        /// <summary>
        /// Only send an alert if it is one of the following types.
        /// If this is empty, it will send an alert for all of the types.
        /// </summary>
        string[] typesToAlert = {
                                    "E", // Error
                                    "W", // Warning
                                    "I", //Info
                                };    

        public override void Init()
        {
            EventLogInfo.TYPEINFO.Events.Insert.After += Insert_After;
        }

        void Insert_After(object sender, CMS.DataEngine.ObjectEventArgs e)
        {
            var eventLogEntry = e.Object as EventLogInfo;

            bool send = true;

            if(codesToAlert.Length > 0)
                send = false;

            if(typesToAlert.Length > 0)
                send = false;

            var code = eventLogEntry.EventCode;
            var type = eventLogEntry.EventType;
            if (codesToAlert.Contains(code) && typesToAlert.Contains(type))
                    send = true;

            if(!send)
                return;

            var currentSiteName = CMS.SiteProvider.SiteInfoProvider.GetSiteName(eventLogEntry.SiteID);
            var message = new EmailMessage() {
                Recipients = "recipient@example.com",
                From = "noreply@gmail.com",
                Subject = string.Format("ALERT: Important Event Log Entry In Your Site {0}", currentSiteName),
                Body = string.Format("ALERT - There has been an important entry in the {0} event log. Description: {1}", currentSiteName, eventLogEntry.EventDescription)
            };

            EmailSender.SendEmail(message);

        }

    }
}

For those wondering how to enable the built in Kentico error notification email, the following excerpt from the Kentico documentation explains it:

  1. Open the Settings application.
  2. Select the System category.
  3. Enter the target e-mail addresses for the notifications into the Error notification e-mail address setting.
  4. Use semicolons to separate multiple addresses.
  5. Type a sender address into the No-reply e-mail address settings.
  6. The notification e-mails use the sender address in their From field.
  7. Save the settings.

-Kentico Manual

I would not recommend turning on this setting in addition to using the class provided above, as you will likely get duplicate emails about errors.

Written on June 26th, 2015 by Dan Walker

Want to read more?

If you’ve enjoyed what you’ve seen so far, you might like some of my other posts. Visit the archive to see all of my past writings.

Blog Archive »
 

About the Author

Dan Walker is a programmer from Grand Rapids, MI. He works at Gordon Food Service.

More About Dan »

Get in touch