Una de las características importantes para una aplicación web que esta corriendo en un ambiente publico es la capacidad de reportar de forma adecuada y con información significativa las condiciones de error que se produzcan, en esta entrada vamos a ver una forma simple de lograr este objetivo utilizando la librería NLog (
básicamente tenemos que aprovecharnos de un evento que se puede interceptar en el Global.asax de nuestra aplicación luego invocar NLog para guardar la información relativa al error que se produjo.
private static Logger logger = LogManager.GetCurrentClassLogger(); void Application_Error(object sender, EventArgs e) { Exception c = Server.GetLastError(); logger.ErrorException("error en web application ", c); }
Luego de esto solo tenemos que asegurarnos de establecer una configuracion adecuada para NLog, ya sea para gurdar archivos de log o mandar mail cada vez que se produce une error etc.
NLog.config
xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}\log.txt" layout="${longdate} ${message} ${exception:format=tostring}" />
targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
rules>
nlog>
es importante tomar en cuenta la propiedad layout para los targets, para asegurarnos de que toda la información disponible se esta almacenando (o enviando por correo).
Descargar Codigo Fuente
ejemplo de mensaje de error en el archivo de log
011-10-23 17:36:40.4809 error en web application System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Exception: todo esta perdido
at errorLoggingTest.About.Page_Load(Object sender, EventArgs e) in c:\users\su\documents\visual studio 2010\Projects\errorLoggingTest\errorLoggingTest\About.aspx.cs:line 14
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.about_aspx.ProcessRequest(HttpContext context) in c:\Users\SU\AppData\Local\Temp\Temporary ASP.NET Files\root\6fa5f363\81e69c33\App_Web_vx1t3hsp.0.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Exception: todo esta perdido
at errorLoggingTest.About.Page_Load(Object sender, EventArgs e) in c:\users\su\documents\visual studio 2010\Projects\errorLoggingTest\errorLoggingTest\About.aspx.cs:line 14
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.about_aspx.ProcessRequest(HttpContext context) in c:\Users\SU\AppData\Local\Temp\Temporary ASP.NET Files\root\6fa5f363\81e69c33\App_Web_vx1t3hsp.0.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Comentarios
Publicar un comentario