一、摘要
- NuGet → Log4net
- 创建工具类Log4Helper.cs
- 创建配置文件Config/Log4net.config
- Web.config
- AssemblyInfo.cs
- 使用
二、详情
- NuGet → Log4net
- 创建工具类Log4Helper.cs
public static class Log4Helper
{
public static void Fatal(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Fatal(message);
else
log.Fatal(message, exception);
}
public static void Error(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Error(message);
else
log.Error(message, exception);
}
public static void Warn(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Warn(message);
else
log.Warn(message, exception);
}
public static void Info(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Info(message);
else
log.Info(message, exception);
}
public static void Debug(Type type, object message, Exception exception = null)
{
ILog log = LogManager.GetLogger(type);
if (exception == null)
log.Debug(message);
else
log.Debug(message, exception);
}
/// <summary>
/// 取得当前源码的哪一行
/// </summary>
/// <returns></returns>
public static int GetLineNum()
{
StackTrace st = new StackTrace(1, true);
StackFrame stackFrame = st.GetFrame(0);
int lineNo = 0;
if (stackFrame != null)
lineNo = stackFrame.GetFileLineNumber();
return lineNo;
}
/// <summary>
/// 取当前源码的源文件名
/// </summary>
/// <returns></returns>
public static string GetCurSourceFileName()
{
StackTrace st = new StackTrace(1, true);
StackFrame stackFrame = st.GetFrame(0);
string fileName = "【未知文件名】";
if (stackFrame != null)
fileName = stackFrame.GetFileName();
return fileName;
}
}
- 创建配置文件 Config/Log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="fatalAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\fatal.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\err.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="warnAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="WARN" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\warn.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\info.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\debug.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<File value="Logs\perf.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="fatalAppender" />
<appender-ref ref="errorAppender" />
<appender-ref ref="warnAppender" />
<appender-ref ref="infoAppender" />
<appender-ref ref="debugAppender" />
</root>
</log4net>
</configuration>
- Web.config添加配置如下
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
</configuration>
- AssemblyInfo.cs中添加代码如下
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]
- 使用
Log4Helper.Debug(typeof(HomeController), "Debug");
Log4Helper.Fatal(typeof(HomeController), "Fatal");
Log4Helper.Error(typeof(HomeController), "Error");
Log4Helper.Info(typeof(HomeController), "Info");
Log4Helper.Warn(typeof(HomeController), "Warn");