使用log4net打印日誌很方便,之前我都是本身寫日誌功能,把錯誤信息寫入到指定文件,使用log4net就省略了這些工做,挺方便的。app
使用log4net須要在.config文件中進行配置debug
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--log4net配置節點-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<log4net debug="false">
<!--普通日誌配置-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\MyWebLog\LogInfo\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".txt"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n日誌時間:%d %n日誌級別:%-5p%n日誌內容:%m%n" />
</layout>
</appender>日誌
<!--異常信息配置-->
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\MyWebLog\DebugError\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".txt"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n異常時間:%d %n異常級別:%-5p%n異常內容:%m%n" />
</layout>
</appender>xml
<!--錯誤日誌配置-->
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\MyWebLog\LogError\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".txt"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n錯誤時間:%d %n錯誤級別:%-5p%n錯誤內容:%m%n" />
</layout>
</appender>對象
<!--致命錯誤配置-->
<appender name="FatalAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\MyWebLog\LogFatal\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".txt"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n致命錯誤時間:%d %n致命錯誤級別:%-5p%n致命錯誤內容:%m%n" />
</layout>
</appender>utf-8
<!--警告信息配置-->
<appender name="WarnAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\MyWebLog\LogWarn\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".txt"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n警告時間:%d %n警告級別:%-5p%n警告內容:%m%n" />
</layout>
</appender>
<!--普通日誌-->
<logger name="LogInfo">
<level value="Info" />
<appender-ref ref="InfoAppender" />
</logger>
<!--異常信息-->
<logger name="LogDebug">
<level value="Debug" />
<appender-ref ref="DebugAppender" />
</logger>
<!--錯誤日誌-->
<logger name="LogError">
<level value="Error" />
<appender-ref ref="ErrorAppender" />
</logger>
<!--致命錯誤-->
<logger name="LogFatal">
<level value="Fatal" />
<appender-ref ref="FatalAppender" />
</logger>
<!--警告信息-->
<logger name="LogWarn">
<level value="Warn" />
<appender-ref ref="WarnAppender" />
</logger>
</log4net>
</configuration>
string
配置文件可使用項目中默認的配置文件,也能夠新建.config。it
1.使用默認的配置文件io
AssemblyInfo.cs中添加[assembly: log4net.Config.XmlConfigurator()],系統在運行時尋找去默認的配置文件中查找log4net的配置節點class
寫一個幫助類,使用單例模式建立日誌對象
/// <summary>
/// log4net幫助類
/// </summary>
public class LogHelper
{
private static ILog logInfo = null;//普通日誌
private static ILog logDebug = null;//異常信息
private static ILog logError = null;//錯誤日誌
private static ILog logFatal = null;//致命錯誤
private static ILog logWarn = null;//警告信息
private LogHelper() { }
/// <summary>
/// 普通日誌
/// </summary>
/// <returns></returns>
public static ILog GetLogInfo()
{
if(logInfo == null)
{
logInfo = LogManager.GetLogger("LogInfo");
}
return logInfo;
}
/// <summary>
/// 異常信息
/// </summary>
/// <returns></returns>
public static ILog GetLogDebug()
{
if (logDebug == null)
{
logDebug = LogManager.GetLogger("LogDebug");
}
return logDebug;
}
/// <summary>
/// 錯誤日誌
/// </summary>
/// <returns></returns>
public static ILog GetLogError()
{
if (logError == null)
{
logError = LogManager.GetLogger("LogError");
}
return logError;
}
/// <summary>
/// 致命錯誤
/// </summary>
/// <returns></returns>
public static ILog GetLogFatal()
{
if (logFatal == null)
{
logFatal = LogManager.GetLogger("LogFatal");
}
return logFatal;
}
/// <summary>
/// 警告信息
/// </summary>
/// <returns></returns>
public static ILog GetLogWarn()
{
if (logWarn == null)
{
logWarn = LogManager.GetLogger("LogWarn");
}
return logWarn;
}
}
調用:
LogHelper.GetLogInfo().Info("輸出信息");
LogHelper.GetLogError().Error("",new Exception());
2.使用新建立的配置文件
public static readonly ILog logInfo = LogManager.GetLogger("LogInfo");//普通日誌
public static readonly ILog logDebug = LogManager.GetLogger("LogDebug");//異常信息
public static readonly ILog logError = LogManager.GetLogger("LogError");//錯誤日誌
public static readonly ILog logFatal = LogManager.GetLogger("LogFatal");//致命錯誤
public static readonly ILog logWarn = LogManager.GetLogger("LogWarn");//警告信息
手動查找路徑下的配置文件,進行讀取,添加一下代碼
/// <summary>
/// 加載log4net的日誌配置文件
/// </summary>
/// <param name="configFilePath">日誌配置文件的完整路徑</param>
public LogHelper(string configFilePath)
{
if (string.IsNullOrWhiteSpace(configFilePath) || !File.Exists(configFilePath))
{
throw new ArgumentNullException("日誌配置文件不存在");
}
XmlConfigurator.Configure(new FileInfo(configFilePath));
}
調用:
LogHelper.logInfo.Info("輸出信息"); LogHelper.logError.Error("輸出信息", new Exception());