log4net registra todos los errores de aplicación no controlados


Puede indicarme algún tutorial o ejemplos sobre cómo puedo registrar todas las excepciones no manejadas que se están produciendo en mi aplicación web mvc utilizando log4net. Gracias

Author: kaivalya, 2009-09-02

5 answers

Pego debajo del código que uso en mi global.asax parece satisfacerme por ahora.. Estoy recibiendo todas las excepciones no controladas en mis archivos de registro generados por log4net..

    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();

        }

    }
 49
Author: kaivalya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-02 19:12:57

Para ASP.NET aplicaciones le gusta usar el evento System.Web.HttpApplication.Error que se coloca en el archivo Global.asax.

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

 5
Author: eljhonb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-11 16:41:12

Quizás te interese ver qué es Aspect Oriented Programming (AOP).

Logramos esto con Post sharp (solo que no usamos log4net, sino un trazador personalizado).

Solo echa un vistazo primero si log4net no tiene algo para este 'out-of-the-box'. No estoy seguro de eso.

 2
Author: Arnis Lapsa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-02 14:38:00

No hay soporte incorporado para esto en log4net. Debe implementar el evento Application_Error en Global.asax y llame a su log4net logger allí. El evento se activará para todos los eventos no controlados en su aplicación.

 2
Author: Peter Lillevold,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-02 16:37:33

Sin ser chistoso aquí tienes.

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

Suponiendo que esté utilizando.net 3.5, podría usar métodos de extensión y extender el sistema.Excepción y agregar un registro (log4net.ILog log), luego donde se detecta una excepción en su aplicación, suponiendo que ha configurado la instancia de registro correctamente, entonces lo hace fácilmente. Su clase de extensión podría crecer un poco, con varias sobrecargas para Debug, Info,Warn, Error y Fatal, etc.

 1
Author: Stuart Grassie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-02 14:30:13