zoukankan      html  css  js  c++  java
  • Working Experience

    正文

    问题: 当前项目已使用 NLog 的情况下再引用使用 NLog 的项目, 出现配置文件冲突, 有一个配置文件不工作
    方法: 使用 LogFactory 代替 LogManager 来获取 Logger 实例

    internal class MyLogManager 
    { 
        // A Logger dispenser for the current assembly (Remember to call Flush on application exit)
        public static LogFactory Instance { get { return _instance.Value; } }
        private static Lazy<LogFactory> _instance = new Lazy<LogFactory>(BuildLogFactory);
    
        // 
        // Use a config file located next to our current assembly dll 
        // eg, if the running assembly is c:path	oMyComponent.dll 
        // the config filepath will be c:path	oMyComponent.nlog 
        // 
        // WARNING: This will not be appropriate for assemblies in the GAC 
        // 
        private static LogFactory BuildLogFactory()
        {
             // Use name of current assembly to construct NLog config filename 
             Assembly thisAssembly = Assembly.GetExecutingAssembly(); 
             string configFilePath = Path.ChangeExtension(thisAssembly.Location, ".nlog"); 
    
             LogFactory logFactory = new LogFactory();
             logFactory.Configuration = new XmlLoggingConfiguration(configFilePath, true, logFactory); 
             return logFactory;
        }
    }
    
    // 使用下面两个方法中的一个获取 Logger 实例
    Logger logger = MyLogManager.Instance.GetCurrentClassLogger();
    Logger logger = MyLogManager.Instance.GetLogger("name");
    

    参考

    Github -> NLog -> Configure component logging

  • 相关阅读:
    c#反射动态创建窗体
    ImageSwitcher 图片切换器
    viewSwitcher 切换视图
    ratingBar 星级评分条
    seekBar拖动滑块
    pythonUDP发送结构体,对齐到C++结构体
    pyqt5界面
    progressbar
    SVN服务器搭建和使用(一)
    关于MBR、GPT以及BIOS引导模式区分
  • 原文地址:https://www.cnblogs.com/zdfffg/p/10879829.html
Copyright © 2011-2022 走看看