asp.net网站记录全局错误

来源:互联网 发布:xampp怎么切换php版本 编辑:程序博客网 时间:2024/05/19 00:40

asp.net网站记录全局错误核心是在Global.asax中注册错误的事件和网站关闭的原因,这样可以便于排查错误。

在发生错误时记录下错误的相关信息核心代码

 void Application_Error(object sender, EventArgs e)    {        // 在出现未处理的错误时运行的代码        Exception ex = Server.GetLastError().GetBaseException();        new DHC.EAS.Common.AppException("当前的应用发生错误", ex);        HttpContext c = HttpContext.Current;        if (c!=null)        {            new DHC.EAS.Common.AppException("当前的应用发生错误"+GetlogInfo(c));        }        //处理完及时清理异常         //   Server.ClearError();            }  protected string GetlogInfo(HttpContext context)    {        string text ="";        if (context.Request.UserAgent != null)        {            string UserAgent = context.Request.UserAgent;                     text += ",UserAgent=" + context.Request.UserAgent;        }        string sourceurl = string.Empty;        if (context.Request.UrlReferrer != null)        {            sourceurl = context.Request.UrlReferrer.LocalPath.ToString().ToLower().Trim();        }        text += ",sourceurl=" + sourceurl;        if (context.Request.Browser != null)        {            text += ",UserHostAddress=" + context.Request.Browser;        }        if (context.Request.RawUrl != null)        {            text += ",RawUrl=" + context.Request.RawUrl;        }        if (context.Request.Url != null)        {            text += ",Url=" + context.Request.Url;        }        if (context.Request.UserHostName != null)        {            text += ",UserHostName=" + context.Request.UserHostName;        }        if (context.Request.UserLanguages != null)        {            text += ",UserLanguages=" + context.Request.UserLanguages;        }        if (context.Request.UserHostAddress != null)        {            text += ",UserHostAddress=" + context.Request.UserHostAddress;        }        string formStr = "";        foreach (string item in context.Request.Form)        {            if (item == "__VIEWSTATE")                continue;            formStr += "," + item + "=" + context.Request.Form[item];        }        text += ",formStr=" + formStr;        //string HttpCookieStr = "";        //foreach (HttpCookie item in context.Request.Cookies)        //{        //    HttpCookieStr += ",Name=" + item.Name + ",Value=" + item.Value;        //}        //text += ",HttpCookieStr=" + formStr;        return text;    }

在网站停止时,记录下停止的原因和相关的信息。

 void Application_End(object sender, EventArgs e)    {        //  在应用程序关闭时运行的代码        DHC.EAS.Common.LogInfo.Info("当前的应用被关闭");        new DHC.EAS.Common.AppException("当前的应用被关闭");              RecordEndReason();    }    // <summary>     /// 记录网站停止运行原因     /// </summary>     protected void RecordEndReason()    {        HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField,        null,        null,        null);        if (runtime == null)            return;        string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage",        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField,        null,        runtime,        null);        string shutDownStack = (string)runtime.GetType().InvokeMember(        "_shutDownStack",        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField,        null,        runtime,        null);        string reasonString = "网站Application_End,停止运行,shutDownMessage=" + shutDownMessage + ",shutDownStack=" + shutDownStack;        new DHC.EAS.Common.AppException(reasonString);    }


原创粉丝点击