【发布时间】: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