如何让 NLog 输出出现在 Azure 函数的流日志中?

How can I get NLog output to appear in the streaming logs for an Azure Function?

我有一个简单的 Azure 函数,我希望能够在流日志 window 和 Application Insights 中监视日志输出。

到目前为止,我能够在 Application Insights 中看到 NLog 输出,但在流中看不到 window。 Microsoft ILogger 输出出现在两者中。

这是我目前所做的:

到目前为止,结果是:

这是最终的功能代码...

public static class Function1
{
    [FunctionName("LogTest")]
    public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
    {
        // Set up NLOG targets and logger
        var config = new LoggingConfiguration();
        //send logging to application insights     
        config.LoggingRules.Add(
          new LoggingRule("*", LogLevel.Trace, 
          new ApplicationInsightsTarget()));
        //also try to log to Trace output 
        //'RawWrite' is used to try and force output at all log-levels  
        config.LoggingRules.Add(
          new LoggingRule("*", LogLevel.Trace, 
          new TraceTarget {RawWrite = true}));

        LogManager.Configuration = config;
        var nlog = LogManager.GetLogger("Example");

        //log using native
        log.LogInformation($"log:Info"); //appears in live-stream, app-insights
        log.LogError("log:Error");       //appears in live-stream, app-insights
        log.LogTrace("log:Trace");       //appears in live-stream, app-insights (after modifying host.json)

        //log using nlog
        nlog.Info("nlog:info");          //appears in ...........  app-insights
        nlog.Error("nlog:error");        //appears in ...........  app-insights
        nlog.Trace("nlog:trace");        //appears in ...........  app-insights                     

        //say goodbye
        log.LogInformation("log:ending");
    }
}

在此先感谢您的任何建议 - 毫无疑问我遗漏了一些简单的步骤。

感谢 the link 由 Rolf Kristensen
提供 似乎解决方案是使用 MicrosoftILoggerTarget 而不是 TraceTarget.

配置新规则

所以完整的代码是

var config = new LoggingConfiguration();
//ensure that log output is seen in the Streamed Log window
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, 
     new MicrosoftILoggerTarget(azureLog)));
//ensure that log output is sent to Application Insights
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, 
     new ApplicationInsightsTarget()));
LogManager.Configuration = config;
var nlog = LogManager.GetLogger("Example");
nlog.Info("output from nlog");

其中 azureLog 是 ILogger 提供给函数的 运行 方法。

MicrosoftILoggerTarget 可以在 NLog.Extensions.Logging nuget 包中找到。