【问题标题】:Using NLog & Console Logging Together in .Net Core Console App在 .Net Core 控制台应用程序中一起使用 NLog 和控制台日志记录
【发布时间】:2020-05-02 01:40:21
【问题描述】:
是否有任何实现可以在封装的记录器中使用 Nlog 和 .Net Core Console Logging。例如,LoggingService 具有 Nlog factory 和 Console Logging 能力。
例如LoggingService.Debug 写入 Nlog 日志文件和控制台。或者有没有其他方法来处理这些类型的问题?
【问题讨论】:
标签:
c#
.net-core
console-application
nlog
【解决方案1】:
如果我误解了这个问题,请告诉我,但 NLog 本身支持写入多个目标(即控制台和日志文件):
每https://github.com/nlog/nlog/wiki/Tutorial#configure-nlog-targets-for-output:
var config = new NLog.Config.LoggingConfiguration();
// Targets where to log to: File and Console
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "file.txt" };
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
// Rules for mapping loggers to targets
config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole);
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
// Apply config
NLog.LogManager.Configuration = config;
使用 NET Core,您还可以通过 Microsoft.Logging.Extensions 的 NLog 提供程序利用 Microsoft 的日志记录抽象:
https://github.com/NLog/NLog.Extensions.Logging