最近在项目中使用log4net,配置起来有点麻烦,在参考了网上的资料后,按自己喜好做了一份配置文件,保存在此,以备复制粘贴。 (- -|)
在Config文件根下增加以下配置:
代码
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="InfoAppender" />
</root>
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="****%-5level [%date][Thread ID: %thread]: %message%newline" />
</layout>
</appender>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="ERROR" />
</filter>
</log4net>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="InfoAppender" />
</root>
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy-MM-dd" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="****%-5level [%date][Thread ID: %thread]: %message%newline" />
</layout>
</appender>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="ERROR" />
</filter>
</log4net>
如果是Web项目,新建一个Global.asax文件,并在Application_Start方法里增加:log4net.Config.DOMConfigurator.Configure();
如果是WinForm项目,打开AssemblyInfo.cs文件,并增加[assembly: log4net.Config.DOMConfigurator()]
一个辅助类:
代码
using System;
using System.Collections.Generic;
using log4net;
public enum LogLevel
{
Debug,
Info,
Warning,
Error,
Fatal
}
/// <summary>
/// Summary description for LogHelper
/// </summary>
public class LogHelper
{
private static readonly ILog log = LogManager.GetLogger("loginfo");
private LogHelper()
{ }
public static void Write(LogLevel level, string message)
{
Write(level, message, null, null);
}
public static void Write(LogLevel level, Exception e)
{
Write(level, null, null, e);
}
public static void Write(LogLevel level, string message, Exception e)
{
Write(level, message, null, e);
}
public static void Write(LogLevel level, string message, IDictionary<string, string> additionalInfo)
{
Write(level, message, additionalInfo);
}
public static void Write(LogLevel level, string message, IDictionary<string, string> additionalInfo, Exception e)
{
string formattedMessage;
switch (level)
{
case LogLevel.Debug:
if (log.IsDebugEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Debug(formattedMessage, e);
}
break;
case LogLevel.Info:
if (log.IsInfoEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Info(formattedMessage, e);
}
break;
case LogLevel.Warning:
if (log.IsWarnEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Warn(formattedMessage, e);
}
break;
case LogLevel.Error:
if (log.IsErrorEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Error(formattedMessage, e);
}
break;
case LogLevel.Fatal:
if (log.IsFatalEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Fatal(formattedMessage, e);
}
break;
default:
throw new ArgumentException(string.Format("type '{0}' not add to Write method", level));
}
}
private static string FormatOutputMessage(string message, IDictionary<string, string> additionalInfo)
{
if (additionalInfo == null || additionalInfo.Count == 0)
return message;
System.Text.StringBuilder buffer = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(message))
buffer.AppendLine(message);
foreach (KeyValuePair<string, string> kvp in additionalInfo)
buffer.AppendLine(kvp.Key + " = " + kvp.Value);
return buffer.ToString();
}
}
using System.Collections.Generic;
using log4net;
public enum LogLevel
{
Debug,
Info,
Warning,
Error,
Fatal
}
/// <summary>
/// Summary description for LogHelper
/// </summary>
public class LogHelper
{
private static readonly ILog log = LogManager.GetLogger("loginfo");
private LogHelper()
{ }
public static void Write(LogLevel level, string message)
{
Write(level, message, null, null);
}
public static void Write(LogLevel level, Exception e)
{
Write(level, null, null, e);
}
public static void Write(LogLevel level, string message, Exception e)
{
Write(level, message, null, e);
}
public static void Write(LogLevel level, string message, IDictionary<string, string> additionalInfo)
{
Write(level, message, additionalInfo);
}
public static void Write(LogLevel level, string message, IDictionary<string, string> additionalInfo, Exception e)
{
string formattedMessage;
switch (level)
{
case LogLevel.Debug:
if (log.IsDebugEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Debug(formattedMessage, e);
}
break;
case LogLevel.Info:
if (log.IsInfoEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Info(formattedMessage, e);
}
break;
case LogLevel.Warning:
if (log.IsWarnEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Warn(formattedMessage, e);
}
break;
case LogLevel.Error:
if (log.IsErrorEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Error(formattedMessage, e);
}
break;
case LogLevel.Fatal:
if (log.IsFatalEnabled)
{
formattedMessage = FormatOutputMessage(message, additionalInfo);
log.Fatal(formattedMessage, e);
}
break;
default:
throw new ArgumentException(string.Format("type '{0}' not add to Write method", level));
}
}
private static string FormatOutputMessage(string message, IDictionary<string, string> additionalInfo)
{
if (additionalInfo == null || additionalInfo.Count == 0)
return message;
System.Text.StringBuilder buffer = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(message))
buffer.AppendLine(message);
foreach (KeyValuePair<string, string> kvp in additionalInfo)
buffer.AppendLine(kvp.Key + " = " + kvp.Value);
return buffer.ToString();
}
}