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