1、添加log4net dll
2、在文件AssemblyInfo.cs加入一句代码,AssemblyInfo.cs的路径所在Properties目录下添加代码如下:// 日志组件配置
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.config", Watch = true)]
3、//代码中调用<P></P>//private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType);<P> log4net.ILog log= log4net.LogManager.GetLogger(this.GetType());</P><P> logger.Debug("调试");
logger.Error("这是一个错误日志");
logger.Fatal("这是一个致命的错误日志");
logger.Warn("这是一条警告日志");
logger.Info("这是一条普通信息");
4、配置文件</P><P><?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender type="log4net.Appender.RollingFileAppender,log4net">
<param value="Logs/Log.log" />
<param value="true" />
<param value="10" />
<param value="10MB" />
<param value="Size" />
<param value="true" />
<param value="yyyy-MM-dd HH:mm" />
<layout type="log4net.Layout.PatternLayout">
<param value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender><!-- Print only messages of level DEBUG or above in the packages -->
<logger>
<level value="DEBUG"/>
</logger>
<appender type="log4net.Appender.RollingFileAppender,log4net" >
<param value="logs/" />
<param value="true" />
<param value="Date" />
<param value=""Logs_"yyyyMMdd".txt"" />
<param value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param value="%d [%t] %-5p %c - %m%n" />
<param value="----header------" />
<param value="----footer------" />
</layout>
</appender>
<appender type="log4net.Appender.ConsoleAppender,log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</P>
<root>
<!--<level value="DEBUG" />-->
<priority value="ERROR" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
vinform程序配置log4net日志输出
内容版权声明:除非注明,否则皆为本站原创文章。