0
votes

NLog + Azure Functions 3.1

Azure function Startup class

public override void Configure(IFunctionsHostBuilder builder)
 {
    var logger = LogManager.Setup()
               .SetupExtensions(e => e.AutoLoadAssemblies(false))
               .LoadConfigurationFromFile(currentDirectory + Path.DirectorySeparatorChar + 'NLog.config')
               .GetLogger(configuration.GetSection('logging:nlog:defaultloggername')?.Value);
  
    builder.Services.AddLogging((logger) =>
             {
                 //logger.ClearProviders();
                 logger.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                 logger.AddNLog();
             }).BuildServiceProvider(); 
}

//NLog.config

<variable name='commonLayout' value='${longdate}|${logger}|${uppercase:${level}}|${message}, ${all-event-properties:format=[key]=[value]:separator=, } ${exception}' />

Azure Function

public FunctionA(ILogger<FunctionA> logger){}

Structured logging does not work with Azure function 3.1. The loggername is spitting out FunctionA, how can I change this to use NLog object in the Azure function. Note: I'm using Azure Function 3.1, I can inject NLog in .net core 2.1 though.

1

1 Answers

0
votes

I'm guessing from your question that you want to control the name of the Logger, so you can make better use of NLog-Filtering-Rules.

Instead of doing this:

    public FunctionA(ILogger<FunctionA> logger){}

Have you tried to ask for ILoggerFactory like this:

    public FunctionA(ILoggerFactory logFactory)
    {
        var logger = logFactory.CreateLogger("MyFavoriteLoggerName");
        logger.LogError("Test");
    }

You can also make use of BeginScope or LogEvent properties like EventId_Id to signal origin.