How to setup Azure Function to redirect NLog output to Microsoft ILogger
See original GitHub issueType: Question NLog version 4.7.5 Nlog.Extensions.Logging 1.6.5
The example app contains an explicit setup of LogManager (extensions, two configuration calls) and then the standard LoggingBuilder setup, again passing configuration (which contains the extensions, rules, etc). I’ve seen some other examples referencing this project explicitly assigning LogManager.Configuration with the specific configuration section.
I’m struggling with the setup for configuring with appsettings.json in a .NET Core 3.1 Console App where the newer code uses Microsoft.Extensions.Logging & DI to get Loggers and older code that uses LogManager.GetCurrentClassLogger(). In particular I’m wondering:
-
Why are there so many calls passing configuration, is this initializing two separate use cases (LogManager direct use & NLog through Microsoft.ExtensionsLogging?)
-
What would be the minimal setup for my “some use Microsoft ILogger, some use LogManager” case?
Thanks so much for your help!
James
ConsoleExampleJsonConfig
var logger = LogManager.Setup()
.SetupExtensions(s => s.AutoLoadAssemblies(false))
.SetupExtensions(s => s.RegisterConfigSettings(config))
.LoadConfigurationFromSection(config)
.GetCurrentClassLogger();
.AddLogging(loggingBuilder =>
{
// configure Logging with NLog
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
loggingBuilder.AddNLog(config);
})
Example on StackOverflow
LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
How to redirect output from NLog Loggers to Microsoft ILogger using MicrosoftILoggerTarget.
The same Azure function instance can be reused for multiple calls, so make sure to only setup the MicrosoftILoggerTarget once within the Azure-function-lifetime (Ex. using
FunctionsStartup
)NLog is supporting these 2 standard modes: