如何获取NLog输出以显示在Azure功能的流日志中?

编程入门 行业动态 更新时间:2024-10-20 11:24:30
本文介绍了如何获取NLog输出以显示在Azure功能的流日志中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的Azure功能,我希望能够在流日志窗口和Application Insights中监视日志输出.

I have a simple Azure Function and I'd like to be able to monitor log output in both the streaming log window and in Application Insights.

到目前为止,我能够在Application Insights中看到NLog输出,但在流窗口中却看不到.Microsoft ILogger输出在两者中都显示.

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

Here's what I've done so far:

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

  • Created a basic Azure test function
  • From the Azure portal, I enabled Application Insights
  • Installed NLog and Microsoft.ApplicationInsights.NLogTarget nuget dependencies for the function.
  • Added code to the function to programmatically create an Application Insights nlog target.
  • After reading these questions... Azure Log Streaming with NLog and How to integrate NLog to write log to Azure Streaming log I have also programmatically added an NLog TraceTarget since they suggest that the streaming log mechanism only displayed the Trace ouput stream (not to be confused with the log-level called 'Trace' !)
  • Finally, I have modified host.json to turn the default logging level up to "Trace". This is required in order for the streaming window to show Trace-level output for the Microsoft ILogger and I had thought it might be the reason none of the Nlog output was showing...

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

到目前为止,结果是:

  • 所有Microsoft记录器输出均显示在流窗口和Application Insights日志中.
  • Nlog输出 显示在Application Insights中,但否不会出现在流窗口中.
  • All Microsoft logger output is shown in both the streaming window and Application Insights log.
  • The Nlog output is shown in Application Insights but does NOT appear in the streaming window.

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

Here is the final function code...

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

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

Thanks in advance for any suggestions - no doubt I'm missing some simple step.

推荐答案

感谢看来解决方案是使用 MicrosoftILoggerTarget 而不是 TraceTarget 配置新规则.

Thanks to the link provided by Rolf Kristensen it seems the solution is to configure a new rule using the MicrosoftILoggerTarget rather than the 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 .

where azureLog is the ILogger provided to the Run method of the function.

MicrosoftILoggerTarget .

更多推荐

如何获取NLog输出以显示在Azure功能的流日志中?

本文发布于:2023-11-02 14:35:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   日志   NLog   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!