封装log4net
封装一个log4net日志管理类
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics;
using log4net;
using log4net.Config;
using log4net.Appender;
namespace Demo
{
/// <summary>
/// 日志管理类
/// </summary>
public class Logger
{
/// <summary>
/// Debug委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DDebug(object message);
/// <summary>
/// Info委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DInfo(object message);
/// <summary>
/// Warn委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DWarn(object message);
/// <summary>
/// Error委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DError(object message);
/// <summary>
/// Fatal委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DFatal(object message);
/// <summary>
/// Debug
/// </summary>
public static DDebug Debug
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Debug; }
}
/// <summary>
/// Info
/// </summary>
public static DInfo Info
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Info; }
}
/// <summary>
/// Warn
/// </summary>
public static DWarn Warn
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Warn; }
}
/// <summary>
/// Error
/// </summary>
public static DError Error
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Error; }
}
/// <summary>
/// Fatal
/// </summary>
public static DFatal Fatal
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Fatal; }
}
/// <summary>
/// 静态构造函数
/// </summary>
static Logger()
{
string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
if (File.Exists(path))
{
log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
}
else
{
RollingFileAppender appender = new RollingFileAppender();
appender.Name = "root";
appender.File = "log.txt";
appender.AppendToFile = true;
appender.RollingStyle = RollingFileAppender.RollingMode.Composite;
appender.DatePattern = "yyyyMMdd-HHmm\".txt\"";
appender.MaximumFileSize = "1MB";
appender.MaxSizeRollBackups = 10;
log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%d{yyyy-MM-dd HH:mm:ss,fff}[%t] %-5p [%c] : %m%n");
appender.Layout = layout;
BasicConfigurator.Configure(appender);
appender.ActivateOptions();
}
}
}
}
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics;
using log4net;
using log4net.Config;
using log4net.Appender;
namespace Demo
{
/// <summary>
/// 日志管理类
/// </summary>
public class Logger
{
/// <summary>
/// Debug委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DDebug(object message);
/// <summary>
/// Info委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DInfo(object message);
/// <summary>
/// Warn委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DWarn(object message);
/// <summary>
/// Error委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DError(object message);
/// <summary>
/// Fatal委托
/// </summary>
/// <param name="message">日志信息</param>
public delegate void DFatal(object message);
/// <summary>
/// Debug
/// </summary>
public static DDebug Debug
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Debug; }
}
/// <summary>
/// Info
/// </summary>
public static DInfo Info
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Info; }
}
/// <summary>
/// Warn
/// </summary>
public static DWarn Warn
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Warn; }
}
/// <summary>
/// Error
/// </summary>
public static DError Error
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Error; }
}
/// <summary>
/// Fatal
/// </summary>
public static DFatal Fatal
{
get { return LogManager.GetLogger((new StackTrace()).GetFrame(1).GetMethod().DeclaringType).Fatal; }
}
/// <summary>
/// 静态构造函数
/// </summary>
static Logger()
{
string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
if (File.Exists(path))
{
log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
}
else
{
RollingFileAppender appender = new RollingFileAppender();
appender.Name = "root";
appender.File = "log.txt";
appender.AppendToFile = true;
appender.RollingStyle = RollingFileAppender.RollingMode.Composite;
appender.DatePattern = "yyyyMMdd-HHmm\".txt\"";
appender.MaximumFileSize = "1MB";
appender.MaxSizeRollBackups = 10;
log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%d{yyyy-MM-dd HH:mm:ss,fff}[%t] %-5p [%c] : %m%n");
appender.Layout = layout;
BasicConfigurator.Configure(appender);
appender.ActivateOptions();
}
}
}
}
log4net.config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--type属性的完整格式为:配置节处理器类名,程序集名称,Version=程序集版本号,Culture=区域信息,PublicKeyToken=公钥-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<!--所有logger的基,root的设置在所有logger中都起作用。
当在root和logger中重复设定相同的appender时,你会发现同一日志信息将被记录两次。-->
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<!--日志记录器logger,可以有多个-->
<logger name="AppLogger">
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</logger>
<logger name="Form1">
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</logger>
<!--一个appender可以由多个logger共用,当然一个logger可以指定多个appender。-->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="App.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,fff}[%t] %-5p [%c] - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
</configuration>
<configuration>
<configSections>
<!--type属性的完整格式为:配置节处理器类名,程序集名称,Version=程序集版本号,Culture=区域信息,PublicKeyToken=公钥-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<!--所有logger的基,root的设置在所有logger中都起作用。
当在root和logger中重复设定相同的appender时,你会发现同一日志信息将被记录两次。-->
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<!--日志记录器logger,可以有多个-->
<logger name="AppLogger">
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</logger>
<logger name="Form1">
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</logger>
<!--一个appender可以由多个logger共用,当然一个logger可以指定多个appender。-->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="App.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,fff}[%t] %-5p [%c] - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
</configuration>