正文

问题: 当前项目已使用 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\to\MyComponent.dll
// the config filepath will be c:\path\to\MyComponent.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

最新文章

  1. js小数点失精算法修正
  2. MongoDB-C#驱动基本操作
  3. CentOS Linux VPS安装IPSec+L2TP VPN
  4. Delphi 精选文章地址
  5. CentOS的Redis内存分配策略配置
  6. java gui可见即可得
  7. jitpack让使用第三方依赖库更简单
  8. [js高手之路] es6系列教程 - 新的类语法实战选项卡
  9. DML数据操作语言之谓词,case表达式
  10. [转]CDH QuickStart VM基本使用
  11. 蓝牙 - 小米手环3 NFC版BLE协议研究
  12. 《React Native 精解与实战》书籍连载「Node.js 简介与 React Native 开发环境配置」
  13. elementUI
  14. Python中str()和repr()函数的区别
  15. mysql 远程登录 设置
  16. Brain Rules: 12 Principles for Surviving and Thriving at Work, Home, and School
  17. html active属性
  18. C++多线程环境下注意共享资源的释放顺序
  19. ISSCC 2017论文导读 Session 14:A 288μW Programmable Deep-Learning Processor with 270KB On-Chip Weight
  20. linux c++ 多线程心得

热门文章

  1. Python文件编码不可以使用UTF16
  2. SVN支干合并(转载)
  3. java实现二叉树的构建以及3种遍历方法(转)
  4. webpack笔记1
  5. sublime 相关配置和快捷键
  6. html5--2.7新的布局元素(4)-time
  7. python基础-元组
  8. overflow:hidden真的失效了吗?
  9. div img 垂直水平居中
  10. 书写优雅的shell脚本(八)- 日期格式化