在一个解决方案上将NLog用于多个项目C# [英] Using NLog for several projects on one solution c#

查看:458
本文介绍了在一个解决方案上将NLog用于多个项目C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案中有8个控制台项目(我打算将来将它们转换为服务)互相引用.我打算创建一个ILogger接口和Logger类来封装Nlog方法,因为我不想在每个项目中都引用nlog.并将此接口从Core项目传递给彼此作为构建参数.

I have 8 console projects in one solution(I'm planning to convert them to services in future) referencing each other. I'm planning to create a ILogger interface and Logger class to encapsulate Nlog methods as I don't want to reference nlog in every project. And pass this interface from Core project to every other as build parameter. Something like

using Core
...
ILogger logger = new Logger();

问题是:

1)我可以为每个记录器使用一个配置吗?我在此处.主要是调用者信息属性来获取程序集名称等.因此,我不需要其他日志文件或设置来确定日志的来源.

1) Can I use one config for every logger? I use ideas from here. Mainly caller info attributes to get the assembly name, etc. So I don't need different log files or settings to determine log's origin.

2)而且我应该这样创建ILogger的8个实例,而不是在Core程序集中的静态类上创建并在需要时每次调用它的方法.我已经在所有其他程序集中引用了Core,因此将不进行任何新的引用.我对8个ILogger的关注是同时写入一个文件/数据库.

2) And should I go this way and create 8 instances of ILogger, instead of creating on static class in Core assembly and calling it's methods each time as I need it. I already reference Core in every other assembly, so no new references will be made. My concern with 8 ILoggers is concurrent write to one file/DB.

推荐答案

1)我可以为每个记录器使用一个配置吗?我从这里使用想法.主要是调用者信息属性来获取程序集名称等.因此,我不需要其他日志文件或设置来确定日志的来源.

1) Can I use one config for every logger? I use ideas from here. Mainly caller info attributes to get the assembly name, etc. So I don't need different log files or settings to determine log's origin.

是的,配置应该在解决方案的启动项目中.

Yes, the config should be there in the startup project of the solution.

2)而且我应该这样创建ILogger的8个实例,而不是在Core程序集中的静态类上创建并在需要时每次调用它的方法.我已经在所有其他程序集中引用了Core,因此将不进行任何新的引用.

2) And should I go this way and create 8 instances of ILogger, instead of creating on static class in Core assembly and calling it's methods each time as I need it. I already reference Core in every other assembly, so no new references will be made.

建议每个类实例.您可以使用LogManager.GetLogger(myClassName),否则很难跟踪日志的来源并且很难过滤.

Instance per class is recommend. You could use LogManager.GetLogger(myClassName), otherwise it's difficult the trace where the logs are from and difficult to filter.

我对8个ILogger的关注是同时写入一个文件/数据库.

My concern with 8 ILoggers is concurrent write to one file/DB.

您可以将消息分组在一个缓冲目标中,以便对写进行分组.请参见缓冲包装"目标的文档

You could group the messages in a buffering target so the writes are grouped. See the docs of the Buffering Wrapper target

这篇关于在一个解决方案上将NLog用于多个项目C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆