记录全局错误

asp.net网站记录全局错误

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

参考:https://blog.csdn.net/xuexiaodong009/article/details/76653624


winfrom记录全局错误

在main函数中注册几个事件,记录下错误,便于排查错误。

SetUnhandledExceptionMode

ThreadException

UnhandledException

 static class Program
    {
        private static Mutex singleton;  
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {//处理未捕获的异常   
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常   
                Application.ThreadException += Application_ThreadException;
                //处理非UI线程异常   
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                log4net.Config.XmlConfigurator.Configure();             
                bool has = Check();
                if (has)
                {
                  //  Form form = new FrmMain();
                      Form form = new FormMain();
                   // Form form = new ExAlarmForm();
                 
                    form.FormClosed += new FormClosedEventHandler(form_FormClosed);
                    Application.Run(form);
                }
                else
                {
                    MessageBox.Show("程序已启动。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
                }
            }
            catch (Exception ex)
            {
                LogInfo.Error("系统异常", ex);
                MessageBox.Show("系统出现未知异常,请重启系统!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;
            if (ex != null)
            {
                LogInfo.Error("系统异常CurrentDomain_UnhandledException", ex);
            }
 
            MessageBox.Show("系统出现未知异常,请重启系统!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var ex = e.Exception;
            if (ex != null)
            {
                LogInfo.Error("系统异常Application_ThreadException", e.Exception);
            }
            MessageBox.Show("系统出现未知异常,请重启系统!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
        }
        static void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (singleton != null)
            {
                singleton.Close();
            }
            LogInfo.Error("系统关闭");
        }
        private static bool Check()
        {
            bool has = false;
            singleton = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out has);
            //   Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;  
            return has;
        }  
    }

出处:https://blog.csdn.net/xuexiaodong009/article/details/76653306

原文地址:https://www.cnblogs.com/mq0036/p/9413584.html