运行环境VS2008 .NET Framework2.0/3.5
先从官网下载Log4net.dll ,这里用的版本是1.2.10.0的。
配置:
①右键添加Log4net.dll 引用
②AssemblyInfo.cs里面添加一句:
- // For Log4net
- [assembly: log4net.Config.XmlConfigurator( ConfigFile = @"log4net.xml" , Watch = false )]
③在项目bin/Debug目录下面添加配置文件:log4net.xml与②中的名字对应
log4net.xml 里面的内容:
其中<param name="File" value="MyLog1.Log" /> 是定义日志文件的名字
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.2" />
- </configSections>
- <log4net>
- <logger name="MyLog">
- <level value="ALL" />
- <appender-ref ref="ConsoleAppender" />
- <appender-ref ref="LogFileAppender" />
- </logger>
- <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
- <layout type="log4net.Layout.PatternLayout">
- <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
- </layout>
- </appender>
- <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
- <param name="File" value="MyLog1.Log" />
- <param name="AppendToFile" value="true" />
- <layout type="log4net.Layout.PatternLayout">
- <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
- </layout>
- </appender>
- </log4net>
- </configuration>
④然后在控制台或窗体程序里加两句:
log4net.ILog logger = log4net.LogManager.GetLogger( "MyLog" );
logger.Debug( "Hello, Log4Net..." );
运行一下,bin/Debug下面的日志文件里面就有信息了。
转载于:https://blog.51cto.com/dxhll/920859