【问题标题】:Azure Function Integration of Serilog with Application Insights, logs visible in Search but are not appearing in Failures events timelineSerilog 与 Application Insights 的 Azure 功能集成,日志在搜索中可见但未出现在故障事件时间线中
【发布时间】:2021-06-01 06:19:51
【问题描述】:

我正在尝试将 Serilog 与 Application Insights sink 一起用于记录目的。我可以在 Azure 门户 (Application Insights) 的搜索栏中看到日志,但如果我们在失败或性能选项卡中查看事件的时间线,则看不到相同的日志。谢谢

下面是在 FunctionStartup 中注册 Logger 的代码,然后注入到 Function 中进行日志记录:

var logger = new LoggerConfiguration()
                               .Enrich.FromLogContext()
                               .Enrich.WithProperty("ApplicationName", "testApp")
                               .Enrich.WithProperty("Environment", "Dev")                               
                               .WriteTo.ApplicationInsights(GetTelemetryClient("Instrumentationkey"), TelemetryConverter.Traces)
                               .CreateLogger();
builder.Services.AddSingleton<ILogger>(logger);

Telementory 客户端正在从辅助方法中获取:

public static TelemetryClient GetTelemetryClient(string key)
        {
            var teleConfig = new TelemetryConfiguration { InstrumentationKey = key };

            var teleClient = new TelemetryClient(teleConfig);

            return teleClient;
        }

host.json

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingExcludedTypes": "Request",
            "samplingSettings": {
                "isEnabled": true
            }
        }
    }
}

【问题讨论】:

  • WriteTo.ApplicationInsights(..., elemetryConverter.Traces),这意味着日志都被写入trace,所以你肯定看不到失败。跨度>
  • 是的,日志正在进入跟踪但在故障时间线中不可见
  • 失败时间线是什么?应用洞察中的故障刀片?
  • 是的故障刀片,它还显示日志的时间线/遥测,其中包含这些自定义跟踪和其他异常。
  • 根据我的测试,在我设置 WriteTo.ApplicationInsights` 并且我的应用程序遇到异常后,应用程序洞察力可以以这种格式捕获它。给它你的意思的时间表? i.stack.imgur.com/7EQ5n.png

标签: .net-core azure-functions azure-application-insights serilog


【解决方案1】:

我明白你的意思,请允许我在这里总结一下我的测试结果。

首先,failure blade 并非旨在提供用于跟踪细节的时间线(异常发生之前发生的情况),而是显示所有异常、错误发生的频率、受影响的用户数量等,更有可能站在高处看到整个节目。

为了实现您的目标,我认为您可以在 Logs 刀片中使用此 kql 查询或在事务刀片中查看它。

union traces, requests,exceptions
| where operation_Id == "178845c426975d4eb96ba5f7b5f376e1"

基本上,我们可以在执行链中添加许多日志,例如在控制器中,记录输入参数,然后记录数据合并或格式化的结果,将异常信息记录在catch,这是我的测试代码。我在故障刀片中看不到任何其他信息,但在事务刀片中,我可以看到时间线。

public class HelloController : Controller
    {
        public string greet(string name)
        {
            Log.Verbose("come to greet function");
            Log.Debug("serilog_debug_info");
            Log.Information("greet name input " + name);
            int count = int.Parse(name);
            Log.Warning("enter greet name is : {0}", count);
            return "hello " + name;
        }
    }

我们可以很容易地发现,整个链共享相同的operationId,并且通过所有这些日志,我们可以查明错误的行代码。顺便说一句,如果我用 try/catch 包围代码,则故障刀片中不会捕获异常。

===================================

使用Serilog integrate app insights,我们需要将serilog发送到应用洞察,我们会在事务搜索中看到很多Traces,所以最好将MinimumLevel设置为信息和更高。下面的截图是我的日志详情,我们也可以通过 operationId 使用 kql 查询来查看全链。

【讨论】:

  • 能否请您尝试在上述代码中将 Serilog 与 Application Insights 集成,然后尝试日志信息?同样在信息方面,我曾经查看所有自定义或非自定义日志。但是在 Azure 或 .NET 中更新的某些内容可能没有在故障刀片中显示信息,而不仅仅是在事务搜索中
  • 我这边测试过,使用serilog可以看到更多的日志信息,但是还是包含了整个执行链。
  • 您能否分享一下您在哪里将 Serilog 与代码中的应用程序洞察力集成在一起的 sn-p?您的 host.json 中还有任何内容吗?
  • 根据我的测试,这是真正的应用程序洞察力无法跟踪功能。这似乎是一个错误,请参阅stackoverflow.com/a/52717627/14574199
【解决方案2】:

您可以按照 Azure Application Insights 在其 GitHub 存储库上提供的解决方案轻松解决此问题,根据此 Github Issue,您可以使用 DI 配置 TelemetryConfiguration,即

services.Configure<TelemetryConfiguration>(
 (o) => {
   o.InstrumentationKey = "123";
   o.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
 });

或者您可以像这样手动配置它:

var config = TelemetryConfiguration.CreateDefault();
var client = new TelemetryClient(config);

因此,在您的代码中,您必须将 GetTelemetryClient

public static TelemetryClient GetTelemetryClient(string key)
    {
        var teleConfig = new TelemetryConfiguration { InstrumentationKey = key };

        var teleClient = new TelemetryClient(teleConfig);

        return teleClient;
    }

到这里

public static TelemetryClient GetTelemetryClient(string key)
    {
        var teleConfig = TelemetryConfiguration.CreateDefault();

        var teleClient = new TelemetryClient(teleConfig);

        return teleClient;
    }

【讨论】:

    【解决方案3】:

    为了使用上面答案中提到的用于 Azure Functions 的遥测配置进行日志记录,我们只需要像下面的 sn-p 那样更新函数,并且在部署时它应该自己获取 Instrumentation 密钥

    public static TelemetryClient GetTelemetryClient()
     {
       var teleConfig = TelemetryConfiguration.CreateDefault();
        
       var teleClient = new TelemetryClient(teleConfig);
        
       return teleClient;
     }
    

    但要在本地运行以及在 Azure 上部署后运行。我们需要在函数 Startup 中添加这样的东西,并去掉上面的 Function。

    builder.Services.Configure<TelemetryConfiguration>((o) =>
                    {
                       o.InstrumentationKey = "KEY";
                       o.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
                    });
            
    builder.Services.AddSingleton<ILogger>(sp =>
                    {
                    var logger = new LoggerConfiguration()
                     .Enrich.FromLogContext()
                     .Enrich.WithProperty("ApplicationName", "TEST")
                     .Enrich.WithProperty("Environment", "DEV")
                     .WriteTo.ApplicationInsights( 
            
                     sp.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces).CreateLogger(); 
            
                     return logger;
                   });
    

    之后,我们只需要在我们的 classes/azure 函数中使用典型的 DI 即可使用 ILogger

    public class Test{
          public ILogger _log;
          public void Test(ILogger log){
            _log=log;
          }
       }
    

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 2021-04-18
      • 2021-09-21
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多