winform wpf 同时使用log4net出错处理

 

最近有个项目,主要是Winform开发,但其中会使用wpf的用户控件,其都要使用log4net进行日志记录,在编译的过程中会报如下错误:

error MC1000: 未知的生成错误“因为没有预加载,所以无法解析程序集“log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a”的依赖项。在使用 ReflectionOnly API 时,必须通过 ReflectionOnlyAssemblyResolve 事件预加载或按需要加载依赖程序集。”

 

经排查,是因为log4net配置的问题,我日志类如下:

/**************************************************************
 * 类名:Log4Allen.cs
 * 描述:日志类,用于记录错误、异常、调试日志
 * 创建者:Allen
 * 时间:Jan 9,2016
 * 
 * ***********************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace QACDR.Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class Log4Allen
    {
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        #region static void WriteLog(Type t, Exception ex)

        public static void WriteLog(Type t, Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error("Error", ex);
        }

        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        #region static void WriteLog(Type t, string msg)

        public static void WriteLog(Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error(msg);
        }

        #endregion
    }
}

  问题就在于[assembly: log4net.Config.XmlConfigurator(Watch = true)],wpf编译会加载解析log4net,这样就会报错,然后就换成另外一种方式初始化log4net的配置,更改之后的方案如下:

/**************************************************************
 * 类名:Log4Allen.cs
 * 描述:日志类,用于记录错误、异常、调试日志
 * 创建者:Allen
 * 时间:Jan 9,2016
 * 
 * ***********************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace QACDR.Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class Log4Allen
    {
        /// <summary>
        /// 初始化日志
        /// </summary>
        public static void InitLog()
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(
                new System.IO.FileInfo("Config\\log4net.config"));
        }

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        #region static void WriteLog(Type t, Exception ex)

        public static void WriteLog(Type t, Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error("Error", ex);
        }

        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        #region static void WriteLog(Type t, string msg)

        public static void WriteLog(Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error(msg);
        }

        #endregion
    }
}

  完美解决Winform与wpf同时使用log4net。





 

posted on 2016-02-28 09:31  WangAnuo  阅读(898)  评论(0编辑  收藏  举报