首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让NLog输出出现在Azure函数的流日志中?

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

Stack Overflow用户
提问于 2019-05-22 18:40:25
回答 1查看 1.1K关注 0票数 1

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

到目前为止,我可以在应用程序洞察中看到NLog输出,但在流窗口中看不到。Microsoft ILogger输出同时出现在这两个文件中。

以下是我到目前为止所做的工作:

从Azure门户创建了一个基本的Azure测试函数

  • ,我为function.

  • Added代码启用了
  • nlog 和 nuget依赖关系,以便以编程方式创建一个Application Insights nlog target.
    • After阅读这些问题...Azure Log Streaming with NLogHow to integrate NLog to write log to Azure Streaming log我还通过编程方式添加了一个NLog TraceTarget,因为它们建议流日志机制只显示跟踪输出流(不要与称为“跟踪”的日志级别混淆!)
    • 最后,我修改了host.json,将默认日志级别调高为“跟踪”。为了让流窗口显示Microsoft的跟踪级输出,这是必需的,我曾认为这可能是没有显示ILogger输出的原因。

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

到目前为止,结果是:

  • 所有微软记录器输出都显示在流窗口和应用程序洞察日志中。
  • Nlog输出显示在应用程序洞察中,但不会出现在流窗口中。

这是最终的函数代码...

代码语言:javascript
复制
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");
    }
}

提前感谢您的建议--毫无疑问,我遗漏了一些简单的步骤。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-22 23:55:11

感谢Rolf Kristensen提供的the link

似乎解决方案是使用MicrosoftILoggerTarget而不是TraceTarget来配置新规则。

所以完整的代码是

代码语言:javascript
复制
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方法的Run。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56254964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档