Log4Net 在ASP.NET WebForm 和 MVC的全局配置


使用log4net可以很方便地為應用添加日志功能。應用Log4net,開發者可以很精確地控制日志信息的輸出,減少了多余信息,提高了日志記錄性能。同時,通過外部配置文件,用戶可以不用重新編譯程序就能改變應用的日志行為,使得用戶可以根據情況靈活地選擇要記錄的信息。

 

   那么我們如何在Web項目中使用Log4Net呢?

 

配置方面請點擊鏈接跳轉上一筆記:

ASP.NET的錯誤處理機制之二(實例log4net)

這次主要針對上次的錯誤機制講解如何進行Log4Net 在ASP.NET WebForm 和 MVC的全局配置

 

一、MVC中的全局配置

在項目中添加一個全局應用程序類Global.asax,如下圖所示:



 

 

 

 方法中添加如下代碼:

 

    1. protected void Application_Start(object sender, EventArgs e)  
    2. {  
    3.  #region 應用程序啟動時,自動加載配置log4Net
    4.   XmlConfigurator.Configure();
    5.  #endregion
    6.  }  
    7. protected void Application_Error(object sender, EventArgs e)
      {
      #region 捕獲全局異常

      Exception error = Server.GetLastError().GetBaseException();
      Exception ex = Server.GetLastError().GetBaseException();
      string ip = Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR") == null ?
      Request.ServerVariables.Get("Remote_Addr").ToString().Trim() :
      Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR").ToString().Trim();
      string logpath = Server.MapPath("~/Log/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
      StringBuilder builder = new StringBuilder();
      builder.AppendLine(string.Format("========== {0} Application_Error BEGIN ==========", DateTime.Now));
      builder.AppendLine("Ip:" + ip);
      builder.AppendLine("瀏覽器:" + Request.Browser.Browser.ToString());
      builder.AppendLine("瀏覽器版本:" + Request.Browser.MajorVersion.ToString());
      builder.AppendLine("操作系統:" + Request.Browser.Platform.ToString());
      builder.AppendLine("頁面:" + Request.Url.ToString());
      builder.AppendLine("錯誤信息:" + ex.Message);
      builder.AppendLine("錯誤源:" + ex.Source);
      builder.AppendLine("異常方法:" + ex.TargetSite);
      builder.AppendLine("堆棧信息:" + ex.StackTrace);
      builder.AppendLine("========== Application_Error END ===================");

      lock (logpath)
      {
      try
      {
      using (var writer = new StreamWriter(logpath, true))
      {
      ZX.B2C.GoodBaby.Common.LogHelper.WriteLog(typeof(RequestNotification), builder.ToString());
      }
      }
      catch
      {
      // 防止寫文件時,文件被人為打開無法寫入等
      // 記錄日志報錯不做處理,不應影響用戶繼續使用
      }
      }

      Server.ClearError();
      Response.Redirect("/Error/ErrorPath404");//跳出至404頁面

      #endregion
      }

 

 

二、WebForm中的全局配置

 

在項目中添加一個全局應用程序類Global.asax,如下圖所示:

 

 

 

 

 

 方法中添加如下代碼:

 

  1. protected void Application_Start(object sender, EventArgs e)
    {
    #region 應用程序啟動時,自動加載配置log4Net

    XmlConfigurator.Configure();

    #endregion
    }
    /// <summary>
    /// 捕獲全局異常
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void Application_Error(object sender, EventArgs e)
    {
    #region 捕獲全局異常

    Exception error = Server.GetLastError().GetBaseException();
    Exception ex = Server.GetLastError().GetBaseException();
    string ip = Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR") == null ?
    Request.ServerVariables.Get("Remote_Addr").ToString().Trim() :
    Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR").ToString().Trim();
    string logpath = Server.MapPath("~/Log/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
    StringBuilder builder = new StringBuilder();
    builder.AppendLine(string.Format("========== {0} Application_Error BEGIN ==========", DateTime.Now));
    builder.AppendLine("Ip:" + ip);
    builder.AppendLine("瀏覽器:" + Request.Browser.Browser.ToString());
    builder.AppendLine("瀏覽器版本:" + Request.Browser.MajorVersion.ToString());
    builder.AppendLine("操作系統:" + Request.Browser.Platform.ToString());
    builder.AppendLine("頁面:" + Request.Url.ToString());
    builder.AppendLine("錯誤信息:" + ex.Message);
    builder.AppendLine("錯誤源:" + ex.Source);
    builder.AppendLine("異常方法:" + ex.TargetSite);
    builder.AppendLine("堆棧信息:" + ex.StackTrace);
    builder.AppendLine("========== Application_Error END ===================");

    lock (logpath)
    {
    try
    {
    using (var writer = new StreamWriter(logpath, true))
    {
    ZX.B2C.GoodBaby.Common.LogHelper.WriteLog(typeof(RequestNotification), builder.ToString());
    }
    }
    catch
    {
    // 防止寫文件時,文件被人為打開無法寫入等
    // 記錄日志報錯不做處理,不應影響用戶繼續使用
    }
    }

    Server.ClearError();
    Response.Redirect("~/Error.htm");

    #endregion
    }

 

 原文鏈接:

Log4Net 在ASP.NET WebForm 和 MVC的全局配置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM