log4net是.Net下一个非常好用的开源日志记录组件。log4net记录日志的功能非常强大。它可以将日志分不同的等级,以不同的格式,输出到不同的媒介。
该博文主要介绍如何将其分类库使用,日后直接套用。
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
namespace Log4net.Logger
{
public enum LogLevel
{
Debug,
Info,
Warn,
Error,
Fatal
}
}
private static LogUtil instance;
private LogUtil() { }
public static LogUtil Instance
{
get
{
return instance ?? (instance = new LogUtil());
}
}
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public bool IsDebugEnabled
{
get { return log.IsDebugEnabled; }
}
public bool IsInfoEnabled
{
get { return log.IsInfoEnabled; }
}
public bool IsWarnEnabled
{
get { return log.IsWarnEnabled; }
}
public bool IsErrorEnabled
{
get { return log.IsErrorEnabled; }
}
public bool IsFatalEnabled
{
get { return log.IsFatalEnabled; }
}
#region [ Error ]
public void Error(string message)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, message);
}
}
public void Error(string message, Exception exception)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, message, exception);
}
}
public void ErrorFormat(string format, params object[] args)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, format, args);
}
}
public void ErrorFormat(string format, Exception exception, params object[] args)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, string.Format(format, args), exception);
}
}
#endregion
#region [ Fatal ]
public void Fatal(string message)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, message);
}
}
public void Fatal(string message, Exception exception)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, message, exception);
}
}
public void FatalFormat(string format, params object[] args)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, format, args);
}
}
public void FatalFormat(string format, Exception exception, params object[] args)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, string.Format(format, args), exception);
}
}
#endregion
private void Log(LogLevel level, string format, params object[] args)
{
switch (level)
{
case LogLevel.Debug:
log.DebugFormat(format, args);
break;
case LogLevel.Info:
log.InfoFormat(format, args);
break;
case LogLevel.Warn:
log.WarnFormat(format, args);
break;
case LogLevel.Error:
log.ErrorFormat(format, args);
break;
case LogLevel.Fatal:
log.FatalFormat(format, args);
break;
}
}
private void Log(LogLevel level, string message, Exception exception)
{
switch (level)
{
case LogLevel.Debug:
log.Debug(message, exception);
break;
case LogLevel.Info:
log.Info(message, exception);
break;
case LogLevel.Warn:
log.Warn(message, exception);
break;
case LogLevel.Error:
log.Error(message, exception);
break;
case LogLevel.Fatal:
log.Fatal(message, exception);
break;
}
}
private void btnExportMsg_Click(object sender, EventArgs e)
{
LogUtil.Instance.Debug("Debug Info");
LogUtil.Instance.Warn("Warn Info");
LogUtil.Instance.Error("Eorror Info");
LogUtil.Instance.Fatal("Fatal Info");
LogUtil.Instance.Info("Info Info");
}