配置文件实现
配置文件的配置信息
1. 安日期生成文件夹
2. 按大小滚动文件
3. 多进程共享文件读写
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<logger name="RootLogger">
<level value="DEBUG"/>
<appender-ref ref="RootAppender"/>
</logger>
<appender name="RootAppender" type="log4net.Appender.RollingFileAppender">
<maxSizeRollBackups value="100"/>
<maximumFileSize value="10MB"/>设置最大文件大小
<appendToFile value="true"/>
<rollingStyle value="Composite"/>设置滚动模式
<preserveLogFileNameExtension value="true"/>
<Encoding value="UTF-8"/> utf-8编码格式
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 多进程共享读写
<file value="logs\\" />
<DatePattern value="yyyy\\\\MM\\\\dd\\\\'CalLog.log'" />设置按天创建文件夹
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [Thr: %thread %property{NDC}] %logger %-5level %class\%method:%line - %message%newline"/>
</layout>
</appender>
<logger name="ClassLogger">
<level value="DEBUG"/>
<appender-ref ref="ClassAppender"/>
</logger>
<appender name="ClassAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [Thr: %thread %property{NDC}] %logger %-5level %class\%method:%line - %message%newline"/>
</layout>
</appender>
<logger name="CalLogger">
<level value="DEBUG"/>
<appender-ref ref="CalAppender"/>
</logger>
<appender name="CalAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [Thr: %thread %property{NDC}] %logger %-5level %class\%method:%line - %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RootAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
Config文件属性,始终复制
AssemblyInfo.cs 添加下面
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Loger.config", Watch = true)]
代码实现
using System;
using System.Text;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
namespace LogTest
{
public static class GlobalLog
{
public static ILog GetLoger(String logName)
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
Logger loger = hierarchy.GetLogger(logName, hierarchy.LoggerFactory);
if (LogManager.Exists(logName) == null || loger.Appenders.Count == 0)
{
RollingFileAppender fileAppender = CreateAppender(logName);
loger.Hierarchy = hierarchy;
loger.AddAppender(fileAppender);
loger.Level = Level.All;
BasicConfigurator.Configure(hierarchy);
// 设置根Appender,将记录所有日志
// BasicConfigurator.Configure(fileAppender );
}
ILog log = LogManager.GetLogger(logName);
return log;
}
// 生成一个Appender 用于记录日志
public static RollingFileAppender CreateAppender(String logName)
{
String sBasePath = "D:\\tmpLog\\";
RollingFileAppender fileAppender = new RollingFileAppender();
fileAppender.Name = logName;
fileAppender.File = sBasePath;
fileAppender.DatePattern = "yyyy-MM-dd\\\\'" + logName + ".log'"; //注意单引号,识别字符串,否则为乱码或无法存日志
fileAppender.StaticLogFileName = false;
fileAppender.MaxSizeRollBackups = 100;
fileAppender.MaximumFileSize = "10MB";
fileAppender.AppendToFile = true;
fileAppender.RollingStyle = RollingFileAppender.RollingMode.Composite;
fileAppender.LockingModel = new FileAppender.MinimalLock();
fileAppender.Encoding = Encoding.UTF8;
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date %-5level %message%newline";
patternLayout.ActivateOptions();
fileAppender.Layout = patternLayout;
fileAppender.PreserveLogFileNameExtension = true;
fileAppender.ActivateOptions();
return fileAppender;
}
// 只是删除这个ILog下面所有的Appender,其他的ILog不受影响
// 用于同一个Loger记录不同的文件,可以通过删除添加不同Appender的方式实现
public static void RemoveAllAppender(ILog log)
{
((Logger)log.Logger).RemoveAllAppenders();
}
public static void RemoveAllAppender(String logName)
{
if (LogManager.Exists(logName) != null)
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
Logger loger = hierarchy.GetLogger(logName, hierarchy.LoggerFactory);
loger.RemoveAllAppenders();
}
}
public static void RemoveAllLoger()
{
LogManager.GetRepository().ResetConfiguration();
LogManager.GetRepository().Shutdown();
((Hierarchy)LogManager.GetRepository()).Clear();
}
public static String GetLogerFullPath(ILog log)
{
AppenderCollection appc = ((Logger)log.Logger).Appenders;
if (appc.Count > 0)
{
RollingFileAppender appender = (RollingFileAppender)appc[0];
return appender.File;
}
return "";
}
public static String GetLogerName(ILog log)
{
AppenderCollection appc = ((Logger)log.Logger).Appenders;
if (appc.Count > 0)
{
RollingFileAppender appender = (RollingFileAppender)appc[0];
return appender.Name;
}
return "";
}
}
}
调用
ILog SocketLog = GlobalLog.GetLoger("Socket");//代码实现