Log4Net使用学习笔记
1、取得Lognet4
可以从http://logging.apache.org/log4net/下载log4net的源代码。
2、项目中添加lognet4引用
解压软件包后,在解压的src目录下将log4net.sln载入Visual Studio .NET,编译后可以得到log4net.dll。用户要在自己的程序里加入日志功能,只需将log4net.dll引入工程即可。
3、在winform中使用
在app.config中如此这般
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0" />
</configSections>
<appSettings>
<add key="conn" value="Driver={SQLServer};Server=localhost;Database=test;Uid=sa;Pwd=;" />
<add key="oleconn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path;" />
</appSettings>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<param name="Header" value="------------[Header]\r\n"/>
<param name="Footer" value="------------[Footer]\r\n"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}>%n - %m%n" />
</layout>
</appender>
</log4net>
</configuration>
4、配置映射在使用log4net.dll的项目的AssemblyInfo.cs中添加如下:
[assembly:log4net.Config.XmlConfigurator()]
或
[assembly: log4net.Config.DOMConfigurator()]
5、使用
using log4net;
namespace Log4
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Log4net
{
public Log4net()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void test()
{
log4net.ILog log = log4net.LogManager.GetLogger("MyLogger");
log.Debug("hello");
}
}
}
6、封装一个log4的使用类
using System;
using log4net;
namespace LogicLayer
{
/// <summary>
/// Log4 的摘要说明。
/// </summary>
public class Log4
{
private static log4net.ILog log;
public Log4()
{
//
// TODO: 在此处添加构造函数逻辑
//
log=log4net.LogManager.GetLogger("Application");
}
public Log4(string logname)
{
log=log4net.LogManager.GetLogger(logname);
}
#region Error
///
/// 记录一个错误信息
///
/// 信息内容
/// 异常对象
public void Error(string message,Exception e)
{
try
{
log.Error(message,e);
}
catch(Exception err)
{
throw new ApplicationException(String.Format("Logger.Error() failed on logging '{0}'.", message ), err);
}
}
///
/// 记录一个错误信息
///
/// 信息内容
public void Error(string message)
{
try
{
log.Error(message);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Error() failed on logging '{0}'.", message ), e);
}
}
#endregion // Error
#region Warning
///
/// 记录一个警告信息
///
/// 信息内容
/// 异常对象
public void Warning(string message,Exception exception)
{
try
{
log.Warn(message,exception);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Warn() failed on logging '{0}'.", message +"::"+ exception.Message), e);
}
}
///
/// 记录一个警告信息
///
/// 信息内容
public void Warning(string message)
{
try
{
log.Warn(message);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Warn() failed on logging '{0}'.", message), e);
}
}
#endregion // Warning
#region Fatal
///
/// 记录一个程序致命性错误
///
/// 信息内容
/// 异常对象
public void Fatal(string message,Exception exception)
{
try
{
log.Fatal(message,exception);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Fatal() failed on logging '{0}'.", message +"::"+ exception.Message), e);
}
}
///
/// 记录一个程序致命性错误
///
/// 信息内容
public void Fatal(string message)
{
try
{
log.Fatal(message);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Fatal() failed on logging '{0}'.", message), e);
}
}
#endregion // Fatal
#region Info
///
/// 记录信息
///
/// 信息内容
/// 异常对象
public void Info(string message,Exception exception)
{
try
{
log.Info(message,exception);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Info() failed on logging '{0}'.", message +"::"+ exception.Message), e);
}
}
///
/// 记录信息
///
/// 信息内容
public void Info(string message)
{
try
{
log.Info(message);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Info() failed on logging '{0}'.", message), e);
}
}
#endregion // Info
#region Debug
///
/// 记录调试信息
///
/// 信息内容
/// 异常对象
public void Debug(string message,Exception exception)
{
try
{
log.Debug(message,exception);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Debug() failed on logging '{0}'.", message +"::"+ exception.Message), e);
}
}
///
/// 记录调试信息
///
/// 信息内容
public void Debug(string message)
{
try
{
log.Debug(message);
}
catch(Exception e)
{
throw new ApplicationException(String.Format("Logger.Debug() failed on logging '{0}'.", message), e);
}
}
#endregion // Debug
}
}