码迷,mamicode.com
首页 > Windows程序 > 详细

WPF使用Log4net.dll库的demo(转载加个人观点)

时间:2016-04-01 13:00:07      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

  • 原文地址:http://blog.csdn.net/linraise/article/details/50547149
  • 配置文件解析地址:http://blog.csdn.net/pfe_nova/article/details/12225349
  • 1.引入库log4net.dll

  • 2.在AssemblyInfo.cs中添加一行:在AssemblyInfo.cs中添加一行:(其中log4net.config对应配置文件名)

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]

  

 

  • 3.添加log4net.config配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log\logfile.log"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyyMMdd"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="All"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
</configuration>

  

 

  • 4.添加一个公共的日志管理类AppLog.cs
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Config;
using System.IO;


namespace log4net
{
    /// <summary>
    /// 使用Log4net插件的log日志对象
    /// </summary>
    public static class AppLog
    {
        private static ILog log;

        static AppLog()
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
            log = LogManager.GetLogger(typeof(AppLog));
        }

        public static void Debug(object message)
        {
            log.Debug(message);
        }

        public static void DebugFormatted(string format, params object[] args)
        {
            log.DebugFormat(format, args);
        }

        public static void Info(object message)
        {
            log.Info(message);
        }

        public static void InfoFormatted(string format, params object[] args)
        {
            log.InfoFormat(format, args);
        }

        public static void Warn(object message)
        {
            log.Warn(message);
        }

        public static void Warn(object message, Exception exception)
        {
            log.Warn(message, exception);
        }

        public static void WarnFormatted(string format, params object[] args)
        {
            log.WarnFormat(format, args);
        }

        public static void Error(object message)
        {
            log.Error(message);
        }

        public static void Error(object message, Exception exception)
        {
            log.Error(message, exception);
        }

        public static void ErrorFormatted(string format, params object[] args)
        {
            log.ErrorFormat(format, args);
        }

        public static void Fatal(object message)
        {
            log.Fatal(message);
        }

        public static void Fatal(object message, Exception exception)
        {
            log.Fatal(message, exception);
        }

        public static void FatalFormatted(string format, params object[] args)
        {
            log.FatalFormat(format, args);
        }
    }
}
  • 5.在任何你想写日志的地方使用,例如:
AppLog.Info("Info log");
AppLog.Error("Error log");

  


  • 6.经验之谈:不要把项目名称起为log4net,或任何数字在中间的名称,否则引入log4net.dll的时候就会直接报错
  • 7.当运行正常没有创建日志文件或者log = LogManager.GetLogger(typeof(AppLog))中Log对象个字段值为false时,右击log4net.config选择属性-->高级把复制到项目的值改为始终复制
  •  8  类库项目中好像不能实现此打印日志功能,

WPF使用Log4net.dll库的demo(转载加个人观点)

标签:

原文地址:http://www.cnblogs.com/wangboke/p/5344507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!