log4net log all unhandled application errors

后端 未结 5 1994
悲哀的现实
悲哀的现实 2020-12-05 02:49

Can you point me to some tutorial or samples on how I can log all un-handled exceptions that are occurring on my mvc web app using log4net. Thank you

相关标签:
5条回答
  • 2020-12-05 02:58

    There is no built-in support for this in log4net. You should implement the Application_Error event in Global.asax and call your log4net logger there. The event will be triggered for all unhandled events in your app.

    0 讨论(0)
  • 2020-12-05 03:03

    For ASP.NET applications you like to use the System.Web.HttpApplication.Error event which is placed in Global.asax file.

    protected void Application_Error(Object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
    
        // Stop error from displaying on the client browser
        Context.ClearError();
    
        logger.Fatal(ex.ToString);
    
    }
    

    watch Managing unhandled exceptions

    0 讨论(0)
  • 2020-12-05 03:06

    I paste below the code I use on my global.asax that seems to satisfy me for now.. I am getting all unhandled exceptions on my log4net generated log files..

        public class MvcApplication : System.Web.HttpApplication
        {
    
            private static readonly ILog log = LogManager.GetLogger(typeof(MvcApplication));
    
            void Application_Error(Object sender, EventArgs e)
            {
                Exception ex = Server.GetLastError().GetBaseException();
    
                log.Error("App_Error", ex);
            }
    
    
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    "Default",
                    "{controller}/{action}/{id}",
                    new { controller = "Home", action = "Index", id = "" }
                );
    
            }
    
            protected void Application_Start()
            {
                RegisterRoutes(RouteTable.Routes);
                log4net.Config.XmlConfigurator.Configure();
    
            }
    
        }
    
    0 讨论(0)
  • 2020-12-05 03:11

    Without being facetious here you go.

            try
            {
                NotImplementedException nie = new NotImplementedException();
                throw nie;
            }
            catch (Exception e)
            {
                Log.Fatal(e);
            }
    

    Assuming you are using .net 3.5, you could use extension methods, and extend System.Exception and add a Log(log4net.ILog log) method, then wherever you catch an exception in your app, assuming you've setup the Log instance correctly, then you easily do it. Your extension class could grow quite a bit, with various overloads for Debug, Info, Warn, Error and Fatal etc.

    0 讨论(0)
  • 2020-12-05 03:12

    You might be interested to check out what Aspect Oriented Programming (AOP) is.

    We accomplished this with Post sharp (only - we didn't use log4net but custom tracer).

    Just check out first if log4net haven't something for this 'out-of-the-box'. I'm not sure about that.

    0 讨论(0)
提交回复
热议问题