1

I need to setup NLog when Azure Functions instantiates my assembly.

public class Startup : FunctionsStartup {
    public Startup()
    {        
        var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
    }
    public override void Configure(IFunctionsHostBuilder builder) {
        ... other setup code
        builder.Services.AddLogging((loggingBuilder) =>
        {
            loggingBuilder.AddNLog();
        });
    }
}

NLog folks recommend manually shutting down the logger via NLog.LogManager.Shutdown(). The problem is that I am not sure how to catch the instance of Azure shutting my assembly down.

Does Azure expose a Dispose event of some sort that I am not seeing?

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • 1
    Here is some chatter about it: https://stackoverflow.com/questions/36760241/intercept-azure-function-host-shutdown-flush-application-insights-telemetryclie – Andy Aug 18 '20 at 20:54

1 Answers1

1

Looks like you are depending on Microsoft Extension Logging, so I would probably hook into its lifetime-logic (Disable AutoShutdown and enable ShutdownOnDispose)

public class Startup : FunctionsStartup {
    public Startup()
    {
        var logger = LogManager.Setup()
           .SetupExtensions(e => e.AutoLoadAssemblies(false))
           .LoadConfigurationFromFile("nlog.config", optional: false)
           .LoadConfiguration(builder => builder.LogFactory.AutoShutdown = false)
           .GetCurrentClassLogger();
    }

    public override void Configure(IFunctionsHostBuilder builder) {
        ... other setup code
        builder.Services.AddLogging((loggingBuilder) =>
        {
            loggingBuilder.AddNLog(new NLogProviderOptions() { ShutdownOnDispose = true });
        });
    }
}
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70