question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to setup Azure Function to redirect NLog output to Microsoft ILogger

See original GitHub issue

Type: 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:

  1. Why are there so many calls passing configuration, is this initializing two separate use cases (LogManager direct use & NLog through Microsoft.ExtensionsLogging?)

  2. 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
snakefootcommented, Oct 20, 2020

MELTarget rather than providing MEL with NLog

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)

1reaction
snakefootcommented, Oct 20, 2020

NLog is supporting these 2 standard modes:

  • NLog registered as LoggingProvider inside Microsoft Extension Logging-framework (First example)
  • Standard NLog using LogManager directly (Stackoverflow example)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using NLog in Azure Functions
Use the MicrosoftILogger Target to redirect the NLog output to ILogger for the Azure-function. Configure NLog to write to the Console target ...
Read more >
Application Insights logging with .NET - Azure Monitor
To add Application Insights logging to console applications, first install the following NuGet packages: ... The following example uses the ...
Read more >
Using NLog with Azure Functions - Davide Mauri - Medium
I need to be able to redirect NLog logging to Azure Function logs so that they I can take advantage of the existing...
Read more >
NLog, Dependency Injection, and Azure Functions, Oh My!
On line 5, I tell NLog to load the configuration from Environment.CurrentDirectory + Path.DirectorySeparatorChar + "nlog.config" which on my ...
Read more >
NLog in Azure Function Log Streams using nlog.config
Usually the goal is to have the output from Microsoft ILogger to reach NLog Targets. This is done by calling UseNLog() or AddNLog()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found