一、第一种在AssemblyInfo.cs中添加log4net的配置//[assembly: log4net.Config.XmlConfigurator(Watch = true)] watch表示是否监视动态加载配置文件的变动。
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
二、第二种在winform的Program.cs中的namespace上面添加
//[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace WinForm
{
static class Program
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
三、第三种是在启动代码中添加
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
try
{
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//读取log4net配置文件
log4net.Config.XmlConfigurator.Configure();
四、其他就是在程序中调用和配置log4net的输出格式
1、添加log4net dll
2、在文件AssemblyInfo.cs加入一句代码,AssemblyInfo.cs的路径所在Properties目录下
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.config", Watch = true)]
3、//代码中调用
添加代码如下:// 日志组件配置
//private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log4net.ILog log= log4net.LogManager.GetLogger(this.GetType());
logger.Debug("调试");
logger.Error("这是一个错误日志");
logger.Fatal("这是一个致命的错误日志");
logger.Warn("这是一条警告日志");
logger.Info("这是一条普通信息");
4、配置文件
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="Logs/Log.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="10MB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<param name="datePattern" value="yyyy-MM-dd HH:mm" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender><!-- Print only messages of level DEBUG or above in the packages -->
<logger name="WebLogger">
<level value="DEBUG"/>
</logger>
<appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="logs/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value="----header------" />
<param name="Footer" value="----footer------" />
</layout>
</appender>
<appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<!--<level value="DEBUG" />-->
<priority value="ERROR" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
添加代码如下:// 日志组件配置
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.config", Watch = true)]
3、//代码中调用