Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • higgsy 65 posts 92 karma points
    Apr 04, 2013 @ 13:53
    higgsy
    0

    Logging unhandled exceptions

    Hi,

    We're working on a 4.11.5 site. We want to capture any unhandled errors and log them, which I don't believe umbraco implements by default.

    We have tried creating a Module and registering it in the web.config, as below, however it never seems to be called.

        using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using log4net;

     

    /// <summary>

    /// Summary description for UnhandledExceptionHandler

    /// </summary>

    public class UnhandledExceptionHandler : IHttpModule

    {

        private static readonly ILog _logger = LogManager.GetLogger(typeof(UnhandledExceptionHandler));

     

        #region IHttpModule Members

     

        public void Dispose()

        {

            //clean-up code here.

        }

     

        public void Init(HttpApplication context)

        {

            context.Error +=new EventHandler(context_Error);

        }

     

        #endregion

     

        #region Event Handlers

     

        void context_Error(object sender, EventArgs e)

        {

            HttpContext httpContext = HttpContext.Current;

            Exception exception;

            for (exception = httpContext.Server.GetLastError(); exception.InnerException != null; exception = exception.InnerException)

            {

            }

            if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404)

            {

                _logger.Warn("A 404 occured", exception);

            }

            else

            {

                _logger.Error("Error module caught an unhandled exception", exception);

            }

        }

     

        #endregion

    }

    So we switched paths and tried registering an event in an umbraco startup module, which again doesnt ever seem to be executed.

    using System;

    using System.Web;

    using log4net;

    using Umbraco.Core;

    using Umbraco.Web;

    using umbraco.BusinessLogic;

    using umbraco.cms.businesslogic;

    using umbraco.cms.businesslogic.web;

     

    /// <summary>

    /// Summary description for Events

    /// </summary>

    public class Events : IApplicationEventHandler

    {

        private static readonly ILog _logger = LogManager.GetLogger(typeof(UnhandledExceptionHandler));

     

        public void OnApplicationInitialized(UmbracoApplication httpApplication, ApplicationContext applicationContext)

        {

            httpApplication.Error += new System.EventHandler(httpApplication_Error);

        }

     

        void httpApplication_Error(object sender, System.EventArgs e)

        {

            HttpContext httpContext = HttpContext.Current;

            Exception exception;

            for (exception = httpContext.Server.GetLastError(); exception.InnerException != null; exception = exception.InnerException)

            {

            }

            if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404)

            {

                _logger.Warn("A 404 occured", exception);

            }

            else

            {

                _logger.Error("Error module caught an unhandled exception", exception);

            }

        }

     

     

        public void OnApplicationStarted(UmbracoApplication httpApplication, ApplicationContext applicationContext)

        {

     

        }

     

        public void OnApplicationStarting(UmbracoApplication httpApplication, ApplicationContext applicationContext)

        {

     

        }

    }

    On top of that, a lot of our code is in Razor, so how do we actually log an error from a Razor view?

    Any help would be greatly appreciated.

    Thanks

    Al

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Apr 04, 2013 @ 14:18
    Stefan Kip
    0

    Can't you just boost the priority of log4net's config?

    Loggers may be assigned levels. Levels are instances of the log4net.Core.Level class. The following levels are defined in order of increasing priority:

    • ALL
    • DEBUG
    • INFO
    • WARN
    • ERROR
    • FATAL
    • OFF

    Otherwise I'd suggest looking at another logging mechanism, like ELMAH or Raygun.

  • higgsy 65 posts 92 karma points
    Apr 04, 2013 @ 14:29
    higgsy
    0

    Hi kipusoep,

    Thanks for your response.

    Well I had originally changed the priority to ALL, however I still dont see any logs for errors in the code we have written (and I've purposely thrown an error). Likewise log4Net doesnt capture Unhandled exceptions as far as I'm aware.

    I'm not sure why you are suggesting an alternative approach - if I can get the Module or StartupHandler to work I can log errors using the same log4net, and importantly the same appender.

    Any thoughts on why neither of those mechanisms are firing?

    Thanks

    Al

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Apr 04, 2013 @ 15:12
    Stefan Kip
    0

    With the HttpModule it should work, this is a link to the HttpModule of Raygun: https://github.com/MindscapeHQ/raygun4net/blob/master/Mindscape.Raygun4Net/RaygunHttpModule.cs

    Did you register your httpModule in the web.config? In both spots like explained here: http://marshallzhao.blogspot.nl/2012/04/register-httpmodule-in-iis6-and-iis7.html?

    I wouldn't try using umbraco's IApplicationEventHandler, HttpModule is the way to go.

    The reason I'm suggesting something else than log4net, is because ELMAH en Raygun (Raygun rocks imo) are designed for error logging, with features like alerts etc.

Please Sign in or register to post replies

Write your reply to:

Draft