【问题标题】:How to configure ApplicationInsightsTelemetry, in .NET Core Web Api如何在 .NET Core Web Api 中配置 ApplicationInsightsTelemetry
【发布时间】:2021-05-13 14:51:05
【问题描述】:

我正在尝试根据自己的喜好调整 Application Insights。我有一些问题。我是这样安装的。 在 startup.cs 类中:

public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry();
            
    services.AddControllers();
}

在 Program.cs 中:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.AddApplicationInsights("73985d32-dc3b-4a7e-915e-aa7ef37fbef8");
                    logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

所以我避免使用 appsettings.json 配置,因为日志级别是可变的。 我的第一个问题是,如何通过查询数据库来制作动态日志级别? 另一个问题,如何添加自定义参数,输入 customer_name?

最后,如何让 Application Insights 注册我想要的值,使用: 只记录这些函数出来的值,比如有异常就不用注册了。

_logger.LogInformation("Test info");
_logger.LogError(ex, ex.Message);

【问题讨论】:

  • 如果你想对 AI 进行动态日志记录,你必须实现一个ITelemetryProcessor。然后,您的记录器必须始终将消息推送到 AI,然后您的处理器进行过滤(通过省略对 _next.Process(item); 的调用)。
  • 我的回答对你有用吗?

标签: c# .net-core asp.net-core-webapi azure-application-insights


【解决方案1】:

关于您的第一个问题

我认为这是不可能的,有关更多详细信息,您可以查看bowman的答案。

Overriding Log Level for Azure Functions

关于第二个问题

Jitendra Patil 的代码真的很棒,这应该是你需要的。

Adding custom properties for each request in Application Insights metrics

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2018-01-04
    相关资源
    最近更新 更多