在.Net Core控制台应用程序中同时使用NLog和控制台日志记录

问题描述 投票:0回答:1

是否有任何实现可在封装的记录器中使用Nlog和.Net Core控制台记录。例如,LoggingService具有Nlog工厂和控制台日志记录功能。例如,LoggingService.Debug同时写入Nlog日志文件和控制台。还是有其他方法可以解决这类问题?

c# .net-core console-application nlog
1个回答
0
投票

让我知道我是否对该问题有误解,但是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,您还可以通过NLog提供程序为Microsoft.Logging.Extensions利用Microsoft的日志记录抽象:

https://github.com/NLog/NLog.Extensions.Logging

© www.soinside.com 2019 - 2024. All rights reserved.