前言:
記錄系統中的日誌,是一個好的項目產品必備的一個環節。每一個產品最終的目的都是要交予客戶使用,因為程式設計師代碼的開發水平參差不齊,Bug就成為了項目運維成本最大的因素之一。如何降低項目運維的成本呢,最重要的是要縮短開發人員在系統運維排查問題的時間成本。如果你的系統中很好的集成了日誌框架。那麼你可以更快更高效的定位問題、解決問題。從而降低系統運維的時間成本。今天小編給大家介紹Log4net日誌框架如何在項目中 使用,希望能對大家有所幫助。
Log4net 介紹
1、Log4net 是什麼?
Log4net 是 Apache 旗下一個開源的日誌框架的項目,它是Log4j 的一個複製版。Log4net中定義了多種日誌信息輸出模式。它可以根據需要將日誌輸出到控制台、文本文件、windows 日誌事件查看器中、括資料庫,郵件發送,以便我們可以根據日誌快速定位線上系統的問題。
2、Log4net 日誌等級
從高到底分別為:OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL
二、ASP.NET MVC 使用 Log4net 示例
2.1 如何安裝 Log4net Log4net
- 使用Nuget 安裝 Log4net(工具-NuGet包管理器-程序包管理器控制台(N))
- 使用管理NuGet程序包功能找到Log4net進行安裝。
2.2 創建 log4net.config 配置文件 ,並且將該文件的屬性「複製到輸出目錄」修改為 「始終複製。
log4net.config內容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<!--<appender-ref ref="EventLogAppender" />-->
</root>
<!--定義輸出到文件-->
<appender name ="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--定義文件存放位置-->
<param name="File" value ="Logs\"/>
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<!--這是按日期產生文件夾-->
<datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'"/>
<param name="RollingStyle" value ="Date" />
<!--不以獨占方式記錄日誌,僅在記錄每個日誌的最短時間內鎖定,
因為部署到伺服器上遇到了文件被占用無法下載日誌-->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<!--定義輸出格式-->
<!--示例 2018-08-20 12:10:49,348 -線程ID:[21] 日誌級別:[INFO ] : [日誌信息]-->
<!--<conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n錯誤描述:%message%newline %n"/>-->
<conversionPattern value="%n==========
%n【日誌級別】%-5level
%n【記錄時間】%date
%n【線程編號】[%thread]
%n【執行時間】[%r]毫秒
%n【出錯文件】%F
%n【出錯行號】%L
%n【出錯的類】%logger 屬性[%property{NDC}]
%n【錯誤描述】%message
%n【錯誤詳情】%newline"/>
</layout>
<!--過濾級別 FATAL > ERROR > WARN > INFO > DEBUG-->
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<!--定義輸出到 windows 事件中-->
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline"></conversionPattern>
</layout>
</appender>
</log4net>
</configuration><?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net"/>
</configSections>
2.3 新建LogHelper.cs通用日誌類
代碼如下:
public class LogHelper
{
private ILog _log4Net = null;
private const string DEFAULT_LOGGER_NAME = "Logger";
/// <summary>
/// Prevents a default instance of the <see cref="LogWriter"/> class from being created.
/// </summary>
/// <param name="log4NetInstance">The log4net instance to be used.</param>
private LogHelper(ILog log4NetInstance)
{
_log4Net = log4NetInstance;
}
/// <summary>
/// Gets a logger with the specified configuration name.
/// </summary>
/// <param name="configName">Name of the logger in the configuration.</param>
/// <returns>The logger obtained.</returns>
/// <exception cref="System.Configuration.ConfigurationException">Thrown when no logger with the specified configuration name was found.</exception>
public static LogHelper GetLogger(string configName)
{
var logger = LogManager.GetLogger(configName);
if (logger == null)
{
throw new ArgumentException(string.Format("No logger configuration named '{0}' was found in the configuration.", configName), "configName");
}
return new LogHelper(logger);
}
/// <summary>
/// Gets the default.
/// </summary>
public static LogHelper Default
{
get
{
return GetLogger(DEFAULT_LOGGER_NAME);
}
}
/// <summary>
/// Writes an information level logging message.
/// </summary>
/// <param name="message">The message to be written.</param>
public void WriteInfo(object message)
{
_log4Net.Info(message);
}
/// <summary>
/// Writes a warning level logging message.
/// </summary>
/// <param name="message">The message to be written.</param>
public void WriteWarning(object message)
{
_log4Net.Warn(message);
}
/// <summary>
/// Writes a warning level logging message.
/// </summary>
/// <param name="message">The message to be written.</param>
/// <param name="exception">The exception.</param>
public void WriteWarning(object message, System.Exception exception)
{
_log4Net.Warn(message, exception);
}
/// <summary>
/// Writes the error.
/// </summary>
/// <param name="message">The message to be written.</param>
public void WriteError(object message)
{
_log4Net.Error(message);
}
/// <summary>
/// Writes the error level logging message..
/// </summary>
/// <param name="message">The message to be written.</param>
/// <param name="exception">The exception.</param>
public void WriteError(object message, System.Exception exception)
{
_log4Net.Error(message, exception);
}
/// <summary>
/// Writes the fatal error level logging message..
/// </summary>
/// <param name="message">The message to be written.</param>
public void WriteFatal(object message)
{
_log4Net.Fatal(message);
}
/// <summary>
/// Writes the fatal error level logging message..
/// </summary>
/// <param name="message">The message to be written.</param>
/// <param name="exception">The exception.</param>
public void WriteFatal(object message, System.Exception exception)
{
_log4Net.Fatal(message, exception);
}
public void DeleteLog()
{
string logDirPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Log");
if (!Directory.Exists(logDirPath)) return;
int days = 30;
foreach (string filePath in Directory.GetFiles(logDirPath))
{
DateTime dt;
DateTime.TryParse(Path.GetFileNameWithoutExtension(filePath).Replace(@"Log\", "").Replace(".", "-"), out dt);
if (dt.AddDays(days).CompareTo(DateTime.Now) < 0)
{
File.Delete(filePath);
}
}
}
}
2.4 AssemblyInfo.cs 文件配置log4net.config
特別注意:針對為web項目要加載web項目對應的AssemblyInfo.cs文件中去,這一點很關鍵要不然就不能輸出日誌文件
[assembly: log4net.Config.XmlConfigurator
(Watch = true, ConfigFileExtension = "config",
ConfigFile = @"Configs\log4net.config")]
2.5 自定義日誌異常處理文件:MyExceptionFileAttribute.cs
用來處理異常記錄日誌文件,然後可以指定錯誤的跳轉頁面。
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if (!string.IsNullOrWhiteSpace(filterContext.Exception.StackTrace))
{
LogHelper.Default.WriteError(filterContext.Exception.StackTrace);
}
//頁面跳轉到錯誤頁面
filterContext.HttpContext.Response.Redirect("/Error");
}
2.6 Global.asax新增Log4Net的配置信息
Application_Start.cs 方法加入下面兩行代碼:
// 註冊log4net
log4net.Config.XmlConfigurator.Configure();
// 註冊異常日誌文件
GlobalFilters.Filters.Add(new MyExceptionFileAttribute());
測試用法
LoginController.cs 代碼加一個異常
public ActionResult Index()
{
int a = 0;
int b = 6 / a;
return View();
}
效果展示:
結語
本文主要簡單介紹ASP.NET MVC 使用 Log4net 的過程。具體的一些用法大家也可以互相交流,比如:如何錯誤日誌如何發郵件、資料庫的方式存儲、日誌文件異步存儲等等。後續小編會繼續更新相關內容和大家一起學習交流。
個人博客網站:https://programmerblog.xyz