【问题标题】:Custom logging to app insights from .net core从 .net 核心自定义日志记录到应用程序洞察
【发布时间】:2017-11-10 07:45:11
【问题描述】:

我在 .net 核心网络应用中使用 App Insights,我可以成功地在应用洞察日志中看到遥测数据。我现在希望能够推送我自己的日志事件,但似乎无法让它工作(即我的自定义日志事件不会出现在应用洞察力中)。我的代码如下:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddApplicationInsights(app.ApplicationServices);
    loggerFactory.AddDebug();

    var logger = loggerFactory.CreateLogger<Startup>();

    logger.LogInformation("I am a log event");

我也尝试过像这样显式设置日志级别:

loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Debug);

但我从未在应用洞察中看到“我是日志事件”的文字。

谁能告诉我如何解决这个问题?

【问题讨论】:

  • 这种方法现在已经过时了。请看link

标签: c# azure .net-core


【解决方案1】:
  1. 您需要为此添加一个 Application Insights 日志捕获模块。选择适当的包 - 之一:
    • Microsoft.ApplicationInsights.TraceListener(捕获 System.Diagnostics.Trace 调用)
    • Microsoft.ApplicationInsights.EventSourceListener(捕获 EventSource 事件)
    • Microsoft.ApplicationInsights.EtwListener(到 捕获 ETW 事件)
    • Microsoft.ApplicationInsights.NLogTarget
    • Microsoft.ApplicationInsights.Log4NetAppender

文档部分:Explore .NET trace logs in Application Insights

  1. 另外,您可以直接使用TelemetryClient。它有更多选项,不仅仅是记录器使用的TrackTrace
//TelemetryClient telemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();
...
telemetryClient.TrackEvent("<EventName>");
telemetryClient.TrackMetric("<metric name>", 1);
telemetryClient.TrackTrace("message", SeverityLevel.Information);

文档部分:Application Insights API for custom events and metrics

对于现场示例,您可以查看feature test int ApplicationInsights-aspnetcore repo。

【讨论】:

  • 应用洞察可以为日志创建不同的列吗?就像我想发送像 { azure function 1,started, time, GUID, filename, containername, etc } 这样的日志。这将是一行日志。所以我的应用洞察力会根据它创建列。
  • 我想微软建议不要新建 telemetryclient 对象,而是将其作为依赖项传递。可以在配置服务方法中使用 services.AddApplicationInsightsTelemetry() 来完成
猜你喜欢
  • 2017-12-07
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
相关资源
最近更新 更多