記錄全局錯誤

asp.net網站記錄全局錯誤

asp.net網站記錄全局錯誤核心是在Global.asax中註冊錯誤的事件和網站關閉的緣由,這樣能夠便於排查錯誤。asp.net

在發生錯誤時記錄下錯誤的相關信息核心代碼函數

 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/76653624url


winfrom記錄全局錯誤

在main函數中註冊幾個事件,記錄下錯誤,便於排查錯誤。spa

SetUnhandledExceptionMode.net

ThreadException線程

UnhandledExceptioncode

 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/76653306orm

相關文章
相關標籤/搜索