繁体   English   中英

如何使NLog输出显示在Azure函数的流日志中?

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

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

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

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

  • 创建了基本的Azure测试功能
  • 我从Azure门户启用了Application Insights
  • 为该功能安装了NLogMicrosoft.ApplicationInsights.NLogTarget nuget依赖项。
  • 向该函数添加了代码,以编程方式创建Application Insights nlog目标。
  • 在阅读了这些问题之后... 使用NLog进行Azure日志流以及如何将NLog集成到Azure流日志中写入日志我还以编程方式添加了NLog TraceTarget,因为他们建议流日志机制仅显示跟踪输出流(不与称为“跟踪”的日志级别混淆!)
  • 最后,我修改了host.json ,将默认日志记录级别更改为“ Trace”。 这是流媒体窗口显示Microsoft ILogger的跟踪级别输出所必需的,并且我认为这可能是Nlog输出都未显示的原因...

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

到目前为止,结果是:

  • 所有Microsoft记录器输出均显示在流窗口和Application Insights日志中。
  • 该NLOG 输出显示在应用见解,但不会出现在流窗口。

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

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

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

感谢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是提供给该函数的Run方法的ILogger

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

暂无
暂无

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

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