locked
How to handle logging and exception in multi layered application RRS feed

  • Question

  • User1455579919 posted

    Hi Guys,

    I have 3 projects in a solution. 1-web application(webforms) 2-BusinessLogic 3-database access.

    So what I did to handle and save exceptions to the database was make a method exception method to save the <g class="gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="12" data-gr-id="12">ex information</g> in the database in the database access layer. Then call the save exception method in the catch blocks of the methods up to the top level and in the web application layer I've called in the exception method in a static utility class method to use in the <g class="gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="13" data-gr-id="13">aspx</g> pages.

    So my question is it the right approach and how can I handle it in an efficient manner.   

    Thanks

    Saturday, November 10, 2018 2:56 PM

Answers

  • User1120430333 posted

    So my question is it the right approach and how can I handle it in an efficient manner.

    Well the optimal approach here would be to have no try/catch in any of the layers. All exceptions thrown would be handled as unhandled exceptions. All unhandled exceptions would be caught by the global exception handler in the presentation layer when an exception was thrown in any layer.

    Here is a ASP.NET MVC 5 approach using a basecontroller where any exception thrown in any layer is caught by the basecontroller's global exception handler. All MVC controllers in the solution inherit from the basecontroller. 

    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using log4net;
    
    namespace MVC.Controllers
    {
        public abstract partial class BaseController : Controller
        {
            private ILog _logger;
    
            protected BaseController()
            {
                _logger =  LogManager.GetLogger(typeof(BaseController));
            }
    
            protected override void OnException(ExceptionContext filterContext)
            {
                AppException appException = new AppException(Convert.ToString(filterContext.Exception))
                {
                    Type = filterContext.GetType().ToString(),
                    StackTrace = filterContext.Exception.StackTrace,
                    Source = filterContext.Exception.Source,
                    InnerException = Convert.ToString(filterContext.Exception.InnerException)
                };
    
                _logger.Error(appException.ToString());
    
                Server.ClearError();
    
                RedirectToControllers("Home", "Error");
            }
    
            private void RedirectToControllers(string control, string action)
            {
                var routeData = new RouteData();
    
                routeData.Values["controller"] = control;
    
                routeData.Values["action"] = action;
    
                IController controller = new HomeController();
    
                ((IController) controller).Execute(new RequestContext(
                    new HttpContextWrapper(System.Web.HttpContext.Current), routeData));
            }
        }
    }


    .

    .

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 10, 2018 7:59 PM

All replies

  • User475983607 posted

    Hi Guys,

    I have 3 projects in a solution. 1-web application(webforms) 2-BusinessLogic 3-database access.

    So what I did to handle and save exceptions to the database was make a method exception method to save the ex information in the database in the database access layer. Then call the save exception method in the catch blocks of the methods up to the top level and in the web application layer I've called in the exception method in a static utility class method to use in the aspx pages.

    So my question is it the right approach and how can I handle it in an efficient manner.   

    Thanks

    The approach sounds fine.  Are you having a problem? 

    Saturday, November 10, 2018 3:03 PM
  • User1455579919 posted

    Hi AgaveJoe, 

    Well, I'm having no problem. I Just thought whether the approach was right secondly I was also thinking about what happens when the saving exception to the database also fails ? should I make a log file method to call in the exception saving to database method.

    Thanks for taking the time to reply 

    Saturday, November 10, 2018 5:29 PM
  • User1120430333 posted

    So my question is it the right approach and how can I handle it in an efficient manner.

    Well the optimal approach here would be to have no try/catch in any of the layers. All exceptions thrown would be handled as unhandled exceptions. All unhandled exceptions would be caught by the global exception handler in the presentation layer when an exception was thrown in any layer.

    Here is a ASP.NET MVC 5 approach using a basecontroller where any exception thrown in any layer is caught by the basecontroller's global exception handler. All MVC controllers in the solution inherit from the basecontroller. 

    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using log4net;
    
    namespace MVC.Controllers
    {
        public abstract partial class BaseController : Controller
        {
            private ILog _logger;
    
            protected BaseController()
            {
                _logger =  LogManager.GetLogger(typeof(BaseController));
            }
    
            protected override void OnException(ExceptionContext filterContext)
            {
                AppException appException = new AppException(Convert.ToString(filterContext.Exception))
                {
                    Type = filterContext.GetType().ToString(),
                    StackTrace = filterContext.Exception.StackTrace,
                    Source = filterContext.Exception.Source,
                    InnerException = Convert.ToString(filterContext.Exception.InnerException)
                };
    
                _logger.Error(appException.ToString());
    
                Server.ClearError();
    
                RedirectToControllers("Home", "Error");
            }
    
            private void RedirectToControllers(string control, string action)
            {
                var routeData = new RouteData();
    
                routeData.Values["controller"] = control;
    
                routeData.Values["action"] = action;
    
                IController controller = new HomeController();
    
                ((IController) controller).Execute(new RequestContext(
                    new HttpContextWrapper(System.Web.HttpContext.Current), routeData));
            }
        }
    }


    .

    .

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 10, 2018 7:59 PM