【问题标题】:How can I get NLog output to appear in the streaming logs for an Azure Function?如何让 NLog 输出出现在 Azure 函数的流式日志中?
【发布时间】:2019-05-22 10:40:25
【问题描述】:

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

到目前为止,我可以在 Application Insights 中看到 NLog 输出,但在流窗口中看不到。 Microsoft ILogger 输出出现在两者中。

这是我到目前为止所做的:

  • 创建了一个基本的 Azure 测试函数
  • 我从 Azure 门户启用了 Application Insights
  • 为函数安装了 NLogMicrosoft.ApplicationInsights.NLogTarget nuget 依赖项。
  • 向函数添加了代码,以编程方式创建 Application Insights nlog 目标。
  • 读完这些问题...Azure Log Streaming with NLogHow to integrate NLog to write log to Azure Streaming log 我还以编程方式添加了一个 NLog TraceTarget,因为他们建议流式日志机制仅显示 Trace 输出流(不要与调用的日志级别混淆“追踪”!)
  • 最后,我修改了 host.json 以将默认日志记录级别提高到“Trace”。这是流窗口显示 Microsoft ILogger 的跟踪级别输出所必需的,我认为这可能是没有显示 Nlog 输出的原因...

    {
     "version": "2.0",
     "logging": {
       "logLevel": {
         "default": "Trace"
        }
      }
    }
    

到目前为止,结果是:

  • 所有 Microsoft 记录器输出都显示在流式窗口和 Application Insights 日志中。
  • Nlog 输出显示在 Application Insights 中,但显示在流式窗口中。

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

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");
    }
}

提前感谢您的任何建议 - 毫无疑问,我错过了一些简单的步骤。

【问题讨论】:

  • 感谢卡洛斯,但正如我在帖子中已经提到的,我已经阅读了该问答和相关的问答。这两个都是指使用配置文件,而我正在尝试使用(等效)编程接口来做到这一点。
  • 我稍后会更详细地检查这些链接,但只是重申一下... NLog _正在使用与 TraceTarget 完全相同的规则正确记录在 ApplicationInsights 目标上。问题是 TraceTarget 输出没有进入 Azure 门户中的流式日志控制台。第一个链接中有一个有趣的部分,它似乎显示了如何将 NLog 连接到 ILogger 实例,所以我稍后再试。

标签: c# azure-functions nlog


【解决方案1】:

感谢 Rolf Kristensen 提供的 the link
似乎解决方案是使用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 是提供给函数的 Run 方法的ILogger

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

【讨论】:

  • 很高兴MicrosoftILoggerTarget 能够解决您的问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2016-03-01
  • 2016-05-05
相关资源
最近更新 更多