Mumin Ka
Mumin Ka

Reputation: 725

Using NLog & Console Logging Together in .Net Core Console App

Is there any implementation to use Nlog and .Net Core Console Logging in a encapsulated logger. For example, LoggingService has Nlog factory and Console Logging ability. Eg LoggingService.Debug writes to both the Nlog log file and console. Or is there any other approach to handle these type of problems?

Upvotes: 0

Views: 5019

Answers (1)

Adam
Adam

Reputation: 3847

Let me know if I'm misunderstanding the question, but NLog itself supports writing to multiple targets (i.e. both the console and to a log file):

Per 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;

With NET Core, you can also take advantage of Microsoft's logging abstractions via the NLog provider for Microsoft.Logging.Extensions:

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

Upvotes: 4

Related Questions