ASP.NET錯誤處理 日誌記錄

來源:互聯網
上載者:User

Java有log4j記錄錯誤記錄檔,而asp.net可以自己寫一個來記錄錯誤記錄檔!

  1. 先封裝一個類
using System;using System.Collections.Generic;using System.Linq;using System.Web;
using System.IO;using System.Globalization;/// <summary>/// ErrHandler 錯誤記錄檔處理類/// </summary>public class ErrHandler{    public ErrHandler()    {        //        // TODO: Add constructor logic here        //    }    public static void WriteError(Exception e)    {        try        {            ///錯誤記錄檔記錄地址[每天產生一個記錄檔]            string path = "~/Error/" + DateTime.Today.ToString("dd-MM-yy") + ".log";            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))            {                ///不存在該日誌,則建立                File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();            }            ///寫日誌記錄            using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))            {                w.WriteLine("Error Recode:");                w.WriteLine("\tError Time:{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));                w.WriteLine("\tError Address:" + System.Web.HttpContext.Current.Request.Url.ToString());                w.WriteLine("\tTargetSite:" + e.TargetSite);                w.WriteLine("\tError Message:" + e.Message);                w.WriteLine("\tError HelpLink:" + e.HelpLink);                w.WriteLine("\tError StackTrace:" + e.StackTrace);                w.WriteLine("************************************************************************************");                w.WriteLine("\r\n\r\n");                w.Flush();                w.Close();            }        }        catch (Exception ex)        {            WriteError(ex);        }    }}

 

2.. 類寫好了!哪裡調用呢?  建立個Global.asax檔案吧!

<%@ Application Language="C#" %><script runat="server">    void Application_Start(object sender, EventArgs e)     {        // Code that runs on application startup    }        void Application_End(object sender, EventArgs e)     {        //  Code that runs on application shutdown            }            void Application_Error(object sender, EventArgs e)     {         //擷取最後的錯誤        Exception objErr = Server.GetLastError().GetBaseException();        // 這裡開始記錄咯        ErrHandler.WriteError(objErr);
        //清除前一個錯誤
        Server.ClearError();    }    void Session_Start(object sender, EventArgs e)     {        // Code that runs when a new session is started            }    void Session_End(object sender, EventArgs e)     {        // Code that runs when a session ends.         // Note: The Session_End event is raised only when the sessionstate mode        // is set to InProc in the Web.config file. If session mode is set to StateServer         // or SQLServer, the event is not raised.    }       </script>

這樣只要一發生錯誤了,都會記錄到日誌裡面! 你只需定期去檢查錯誤記錄檔了

當然你也可以在錯誤中記錄,如:

try        {            throw new Exception("Error");        }        catch (Exception ex)        {            ErrHandler.WriteError(ex);        }

OVER

Technorati 標籤: asp.net,error

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.