/// <summary>
/// 封装log4net组件,記錄日誌
/// </summary>
/// <remarks>2018-7-9 13:50:14 whl 创建</remarks>
public class Log4Helper
{
static Log4Helper()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Name = "RollingLogFileAppender";
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
//info
RollingFileAppender appender = new RollingFileAppender();
//最小锁定模型以允许多个进程可以写入同一个文件
appender.LockingModel = new FileAppender.MinimalLock();
appender.AppendToFile = true;
appender.File = "log\\Info\\log.txt";
appender.DatePattern = "yyyy_MM_dd".log"";
appender.Layout = patternLayout;
//保留的log文件数量 超过此数量后 自动删除之前的
appender.MaxSizeRollBackups = 200;
appender.MaximumFileSize = "1MB";
appender.RollingStyle = RollingFileAppender.RollingMode.Size;
appender.StaticLogFileName = false;
//获取或设置一个值,该值指示是否在滚动时保存文件扩展名。
appender.PreserveLogFileNameExtension = true;
var filter = new log4net.Filter.LevelRangeFilter();
filter.LevelMin = Level.Info;
filter.LevelMax = Level.Info;
appender.AddFilter(filter);
appender.ActivateOptions();
hierarchy.Root.AddAppender(appender);
//debug
RollingFileAppender appender2 = new RollingFileAppender();
//最小锁定模型以允许多个进程可以写入同一个文件
appender2.LockingModel = new FileAppender.MinimalLock();
appender2.AppendToFile = true;
appender2.File = "log\\Debug\\log.txt";
appender2.DatePattern = "yyyy_MM_dd".log"";
appender2.Layout = patternLayout;
//保留的log文件数量 超过此数量后 自动删除之前的
appender2.MaxSizeRollBackups = 200;
appender2.MaximumFileSize = "1MB";
appender2.RollingStyle = RollingFileAppender.RollingMode.Size;
appender2.StaticLogFileName = false;
//获取或设置一个值,该值指示是否在滚动时保存文件扩展名。
appender2.PreserveLogFileNameExtension = true;
var filter2 = new log4net.Filter.LevelRangeFilter();
filter2.LevelMin = Level.Debug;
filter2.LevelMax = Level.Debug;
appender2.AddFilter(filter2);
appender2.ActivateOptions();
hierarchy.Root.AddAppender(appender2);
//Error
RollingFileAppender appender3 = new RollingFileAppender();
//appender.Name = "RollingLogFileAppender";
//最小锁定模型以允许多个进程可以写入同一个文件
appender3.LockingModel = new FileAppender.MinimalLock();
appender3.AppendToFile = true;
appender3.File = "log\\Error\\log.txt";
appender3.DatePattern = "yyyy_MM_dd".log"";
appender3.Layout = patternLayout;
//保留的log文件数量 超过此数量后 自动删除之前的
appender3.MaxSizeRollBackups = 200;
appender3.MaximumFileSize = "1MB";
appender3.RollingStyle = RollingFileAppender.RollingMode.Size;
appender3.StaticLogFileName = false;
//获取或设置一个值,该值指示是否在滚动时保存文件扩展名。
appender3.PreserveLogFileNameExtension = true;
var filter3 = new log4net.Filter.LevelRangeFilter();
filter3.LevelMin = Level.Error;
filter3.LevelMax = Level.Error;
appender3.AddFilter(filter3);
appender3.ActivateOptions();
hierarchy.Root.AddAppender(appender3);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
//hierarchy.Root.Level = Level.;
hierarchy.Configured = true;
}
public static readonly ILog _Log = LogManager.GetLogger("");
public static void Info(string msg)
{
_Log.Info(msg);
//ILog ilog = LogManager.GetLogger("");
//ilog.Info(msg);
}
public static void Debug(object msg)
{
_Log.Debug(msg);
//ILog ilog = LogManager.GetLogger("");
//ilog.Debug(msg);
}
public static void Debug(object msg, Exception exception)
{
_Log.Debug(msg, exception);
//ILog ilog = LogManager.GetLogger("");
//ilog.Debug(msg, exception);
}
public static void Error(object msg)
{
_Log.Error(msg);
//ILog ilog = LogManager.GetLogger("");
//ilog.Debug(msg, exception);
}
public static void Error(object msg, Exception exception)
{
_Log.Error(msg, exception);
//ILog ilog = LogManager.GetLogger("");
//ilog.Debug(msg, exception);
}
}
用log4net記錄日誌,無需配置文件模式,代碼設置配置
最新推荐文章于 2021-11-10 09:29:08 发布