Click here to Skip to main content
12,402,666 members (73,689 online)
Click here to Skip to main content

Stats

58.1K views
545 downloads
22 bookmarked
Posted

Handling Error Centrally in a SharePoint Application

, 20 May 2008 CPOL
An article on handling errors centrally in a SharePoint application, using IHttpModule.
using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace CentralErrorHandler
{
    public class ErrorModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public void Init(HttpApplication Context)
        {
            Context.Error += new EventHandler(Application_Error);
        }

        #endregion


        public void Application_Error(object sender, EventArgs e)
        {            
            HttpContext Context = HttpContext.Current;            
            Exception exception;
            for (exception = Context.Server.GetLastError(); exception.InnerException != null; exception = exception.InnerException) { }

            String code = DateTime.Now.ToString("ddMMyyyy-HHmmfff");
            String Message = exception.Message + Environment.NewLine + "Stack Trace: " + exception.StackTrace;

            Logger.Write(Message, "General",5, 1, System.Diagnostics.TraceEventType.Critical, code);  
            
            HttpContext.Current.Application.Add("ErrorCode", code);
            HttpContext.Current.Application.Add("LastException", exception);

            String CustomErrorPagePath = System.Configuration.ConfigurationManager.AppSettings["CustomErrorPagePath"];

            ((System.Web.HttpApplication)(sender)).Response.Redirect(CustomErrorPagePath);
        }



    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anupam Ranku
Software Developer (Senior) Gen-i, Australia
Australia Australia
Expertise area: ASP.NET, C#, Microsoft Office SharePoint 2010 & 2007, Web Service, Windows-based Applications etc.

Blog: http://mydevdiary.blogspot.com/

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 20 May 2008
Article Copyright 2008 by Anupam Ranku
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid