简体   繁体   English

Azure Function 3.1 + NLog + 结构化日志记录,LoggerName 问题

[英]Azure Function 3.1 + NLog + Structured Logging, Issues with LoggerName

NLog + Azure Functions 3.1 NLog + Azure 功能 3.1

Azure function Startup class Azure function 启动 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 //NLog.config

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

Azure Function Azure Function

public FunctionA(ILogger<FunctionA> logger){}

Structured logging does not work with Azure function 3.1.结构化日志记录不适用于 Azure function 3.1。 The loggername is spitting out FunctionA, how can I change this to use NLog object in the Azure function.记录器名称正在吐出 FunctionA,我该如何更改它以在 Azure function 中使用 NLog object。 Note: I'm using Azure Function 3.1, I can inject NLog in .net core 2.1 though.注意:我使用的是 Azure Function 3.1,但我可以在 .net 核心 2.1 中注入 NLog。

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.我从您的问题中猜测您想要控制 Logger 的名称,这样您就可以更好地利用 NLog-Filtering-Rules。

Instead of doing this:而不是这样做:

    public FunctionA(ILogger<FunctionA> logger){}

Have you tried to ask for ILoggerFactory like this:您是否尝试过像这样询问ILoggerFactory

    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.您还可以使用BeginScope 或 LogEvent 属性(如 EventId_Id)来指示来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM