在 ASP.Net MVC 中建立自定義 ErrorLogger

步驟 1:建立自定義錯誤記錄過濾器,它將根據 DateWise 在文字檔案中寫入錯誤。

public class ErrorLogger : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {

        string strLogText = "";
        Exception ex = filterContext.Exception;
        filterContext.ExceptionHandled = true;
        var objClass = filterContext;
        strLogText += "Message ---\n{0}" + ex.Message;

        if (ex.Source == ".Net SqlClient Data Provider")
        {
            strLogText += Environment.NewLine + "SqlClient Error ---\n{0}" + "Check Sql Error";
        }
        else if (ex.Source == "System.Web.Mvc")
        {
            strLogText += Environment.NewLine + ".Net Error ---\n{0}" + "Check MVC Code For Error";
        }
        else if (filterContext.HttpContext.Request.IsAjaxRequest() == true)
        {
            strLogText += Environment.NewLine + ".Net Error ---\n{0}" + "Check MVC Ajax Code For Error";
        }
        strLogText += Environment.NewLine + "Source ---\n{0}" + ex.Source;
        strLogText += Environment.NewLine + "StackTrace ---\n{0}" + ex.StackTrace;
        strLogText += Environment.NewLine + "TargetSite ---\n{0}" + ex.TargetSite;
        if (ex.InnerException != null)
        {
            strLogText += Environment.NewLine + "Inner Exception is {0}" + ex.InnerException;//error prone
        }
        if (ex.HelpLink != null)
        {
            strLogText += Environment.NewLine + "HelpLink ---\n{0}" + ex.HelpLink;//error prone
        }

        StreamWriter log;

        string timestamp = DateTime.Now.ToString("d-MMMM-yyyy", new CultureInfo("en-GB"));

        string error_folder = ConfigurationManager.AppSettings["ErrorLogPath"].ToString();

        if (!System.IO.Directory.Exists(error_folder))
        {
            System.IO.Directory.CreateDirectory(error_folder);
        }

        if (!File.Exists(String.Format(@"{0}\Log_{1}.txt", error_folder, timestamp)))
        {
            log = new StreamWriter(String.Format(@"{0}\Log_{1}.txt", error_folder, timestamp));
        }
        else
        {
            log = File.AppendText(String.Format(@"{0}\Log_{1}.txt", error_folder, timestamp));
        }

        var controllerName = (string)filterContext.RouteData.Values["controller"];
        var actionName = (string)filterContext.RouteData.Values["action"];

        // Write to the file:
        log.WriteLine(Environment.NewLine + DateTime.Now);
        log.WriteLine("------------------------------------------------------------------------------------------------");
        log.WriteLine("Controller Name :- " + controllerName);
        log.WriteLine("Action Method Name :- " + actionName);
        log.WriteLine("------------------------------------------------------------------------------------------------");
        log.WriteLine(objClass);
        log.WriteLine(strLogText);
        log.WriteLine();

        // Close the stream:
        log.Close();
        filterContext.HttpContext.Session.Abandon();
        filterContext.Result = new RedirectToRouteResult
         (new RouteValueDictionary 
         {
                 {"controller", "Errorview"}, {"action", "Error"}
         });

    }

}

步驟 2:在將儲存文字檔案的伺服器或本地驅動器上新增物理路徑

<add key="ErrorLogPath" value="C:\ErrorLog\DemoMVC\" />

第 3 步: 使用錯誤 ActionMethod 新增 Errorview 控制器 ****

第 4 步:新增 Error.cshtml 在檢視上檢視和顯示自定義錯誤訊息

步驟 5:在 FilterConfig 類中註冊 ErrorLogger Filter

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new ErrorLogger());
    }
}

步驟 6:在 Global.asax 中註冊 FilterConfig

https://i.stack.imgur.com/E5Ilw.jpg