【发布时间】:2019-04-05 09:08:53
【问题描述】:
我有一个使用应用程序洞察力的 Windows 控制台应用程序。我使用Microsoft.Extensions.DependencyInjection 设置我的班级并添加ILogger。
如果出现异常,我想将其记录到 Application Insights。但由于 Application Insights 不会立即发送跟踪信息,因此我想刷新日志。
有没有办法触发 ILogger 后面的 Application Insights 刷新?
static async Task Main(string[] args)
{
ServiceProvider serviceProvider = ConfigureServices();
var program = serviceProvider.GetService<Program>();
await program.Run();
}
public Program(ILogger<Program> logger)
{
this.logger = logger;
}
private static ServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services
.AddLogging(opt =>
{
opt.AddConsole();
opt.AddApplicationInsights();
})
.AddTransient<Program>()
return services.BuildServiceProvider();
}
public async Task Run()
{
try
{
do.stuff()
}
catch (Exception e)
{
logger.LogError(e, "Exception occured");
// How to flush Application insights here
// Need to wait for Flush (see https://docs.microsoft.com/en-us/azure/azure-monitor/app/console)
await Task.Delay(1000);
throw;
}
}
【问题讨论】:
-
docs.microsoft.com/en-us/azure/azure-monitor/app/… 这个官方文档有一个为控制台应用程序执行 Flush() 的示例
-
@IvanYang 抱歉耽搁了。我昨天忙于计划外的工作,想先试试这个。
标签: c# azure .net-core console-application azure-application-insights