Log4Net 入门示例

在网上看了几个教程,都有一些小问题,几经测试,试验了一下,写一个最简单的能用的出来,希望对初学者能有用!

首先在App.Config里面添加:

 

代码
   
     
< configSections >
< section name ="log4net"
type
="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0" />
</ configSections >
< log4net >
< root >
< level value ="WARN" />
< appender-ref ref ="LogFileAppender" />
< appender-ref ref ="ConsoleAppender" />
</ root >
< appender name ="LogFileAppender" type ="log4net.Appender.FileAppender" >
< file value ="log-file.txt" />
< appendToFile value ="true" />
< layout type ="log4net.Layout.PatternLayout" >
< header value ="[Header]&#13;&#10;" />
< footer value ="[Footer]&#13;&#10;" />
< conversionPattern value ="%date ---[%thread] %-5level '%logger': %message%newline" />
</ layout >
</ appender >
< appender name ="ConsoleAppender" type ="log4net.Appender.ConsoleAppender" >
< layout type ="log4net.Layout.PatternLayout" >
< param name ="ConversionPattern"
value
="%d [%t] %-5p (%c) - %m%n"
/>
</ layout >
</ appender >
</ log4net >

然后在AssemblyInfo.cs添加下面一行。

[assembly: log4net.Config.XmlConfigurator()]

最后就是在代码里使用了。

代码
   
     
public class WorkerThreadExample
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger( " test " );

static void Main()
{
if (log.IsInfoEnabled) log.Fatal( " Application [ConsoleApp] Start " );
log.Info(
" error " );
log.Fatal(
" wadsadasd " );

}

}

 

更详细的信息参见文档和  某位博友的文章,不过这篇文章的config好像有一些问题,主要是param的设置上和文档中不一致。

http://dragon.cnblogs.com/archive/2005/03/24/124254.aspx

你可能感兴趣的:(log)