log4j是Java开发人员所熟悉,他提供了强大的日志记录功能,log4net是为.Net记录日志开发的.使用示例如下(C#):
1、app.config文件
<?XML version="1.0" encoding="utf-8" ?
<configuration
<appSettings
<add key="log4net-config-file" value="log4net.config"/
</appSettings
</configuration
2、log4net.config文件
<?xml version="1.0" encoding="utf-8" ?
<configuration
<!-- This section contains the log4net configuration settings --
<log4net
<!-- Define some output appenders --
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"
<param name="File" value="Log\\LQSS-SS-log.txt" /
<param name="AppendToFile" value="true" /
<param name="MaxSizeRollBackups" value="10" /
<param name="MaximumFileSize" value="100KB" /
<param name="RollingStyle" value="Size" /
<param name="StaticLogFileName" value="true" /
<layout type="log4net.Layout.PatternLayout"
<param name="Header" value="[Header]\r\n" /
<param name="Footer" value="[Footer]\r\n" /
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" /
</layout
</appender
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"
<layout type="log4net.Layout.PatternLayout"
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" /
</layout
</appender
<!-- Setup the root category, add the appenders and set the default level --
<root
<level value="INFO" /
<appender-ref ref="RollingLogFileAppender" /
</root
<root
<level value="DEBUG" /
<appender-ref ref="ConsoleAppender" /
</root
</log4net
</configuration
3、代码文件
using log4net;
using log4net.Config;
class Program
{
private static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary
/// 应用程序的主入口点。
/// </summary
[STAThread]
static void Main(string[] args)
{
try
{
DOMConfigurator.Configure(
new FileInfo(ConfigurationSettings.AppSettings["log4net-config-file"]));
logger.Info("Start application...");
Console.Writeline(“请按 <enter 键退出...");
System.Console.ReadLine();
}
catch (Exception e)
{
logger.Info (e);
}
}
}