在一个解决方案 c# 上将 NLog 用于多个项目
Using NLog for several projects on one solution c#
我在一个解决方案中有 8 个控制台项目(我计划将来将它们转换为服务)相互引用。我打算创建一个 ILogger 接口和 Logger class 来封装 Nlog 方法,因为我不想在每个项目中都引用 nlog。并将这个接口从核心项目传递给每个其他项目作为构建参数。像
using Core
...
ILogger logger = new Logger();
问题是:
1) 我可以为每个记录器使用一个配置吗?我使用来自 here 的想法。主要是调用者信息属性来获取程序集名称等。所以我不需要不同的日志文件或设置来确定日志的来源。
2) 我应该这样做并创建 8 个 ILogger 实例,而不是在核心程序集中创建静态 class 并在需要时每次调用它的方法。我已经在所有其他程序集中引用了 Core,因此不会进行新的引用。我对 8 个 ILogger 的关注是并发写入一个 file/DB.
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) 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.
建议每个 class 个实例。你可以使用LogManager.GetLogger(myClassName)
,否则很难跟踪日志的来源,也很难过滤。
My concern with 8 ILoggers is concurrent write to one file/DB.
您可以将缓冲目标中的消息分组,以便将写入分组。参见 the docs of the Buffering Wrapper target
我在我的解决方案中使用了 using Microsoft.Extensions.Logging
,总共有 11 个项目具有依赖注入。我通常在 startup.cs 文件中配置并将 nlog.config 放在启动项目中。在其他项目中,例如存储库项目,我使用如下:
public class ReceiptRepository : IReceiptRepository
{
private readonly ReceiptContext _context;
private readonly ILogger<ReceiptRepository> _logger;
public ReceiptRepository(ReceiptContext context , ILogger<ReceiptRepository> logger)
{
_context = context;
_logger = logger;
}
public async Task<ReceiptData> CreateReceiptAsync(ReceiptData receipt, CancellationToken token)
{
...
_logger.LogInformation($"ReceiptRepository::CreateReceiptAsync::{receipt.Id}:: is added to repository");
return receipt;
}
我在一个解决方案中有 8 个控制台项目(我计划将来将它们转换为服务)相互引用。我打算创建一个 ILogger 接口和 Logger class 来封装 Nlog 方法,因为我不想在每个项目中都引用 nlog。并将这个接口从核心项目传递给每个其他项目作为构建参数。像
using Core
...
ILogger logger = new Logger();
问题是:
1) 我可以为每个记录器使用一个配置吗?我使用来自 here 的想法。主要是调用者信息属性来获取程序集名称等。所以我不需要不同的日志文件或设置来确定日志的来源。
2) 我应该这样做并创建 8 个 ILogger 实例,而不是在核心程序集中创建静态 class 并在需要时每次调用它的方法。我已经在所有其他程序集中引用了 Core,因此不会进行新的引用。我对 8 个 ILogger 的关注是并发写入一个 file/DB.
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) 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.
建议每个 class 个实例。你可以使用LogManager.GetLogger(myClassName)
,否则很难跟踪日志的来源,也很难过滤。
My concern with 8 ILoggers is concurrent write to one file/DB.
您可以将缓冲目标中的消息分组,以便将写入分组。参见 the docs of the Buffering Wrapper target
我在我的解决方案中使用了 using Microsoft.Extensions.Logging
,总共有 11 个项目具有依赖注入。我通常在 startup.cs 文件中配置并将 nlog.config 放在启动项目中。在其他项目中,例如存储库项目,我使用如下:
public class ReceiptRepository : IReceiptRepository
{
private readonly ReceiptContext _context;
private readonly ILogger<ReceiptRepository> _logger;
public ReceiptRepository(ReceiptContext context , ILogger<ReceiptRepository> logger)
{
_context = context;
_logger = logger;
}
public async Task<ReceiptData> CreateReceiptAsync(ReceiptData receipt, CancellationToken token)
{
...
_logger.LogInformation($"ReceiptRepository::CreateReceiptAsync::{receipt.Id}:: is added to repository");
return receipt;
}