如何在ILogger中刷新应用程序见解

问题描述 投票:0回答:1

我有一个使用应用程序见解的Windows控制台应用程序。我使用Microsoft.Extensions.DependencyInjection设置我的课程并添加ILogger

如果出现异常,我想将其记录到Application Insights。但是,由于Application Insights不会立即发送跟踪,我想要刷新日志。

有没有办法触发ILogger背后的Flush of 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;
            }
        }
c# .net-core console-application azure-application-insights
1个回答
2
投票

请尝试使用方法InMemoryChannel.Flush,代码示例为打击:

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace ConsoleApp3netcore
{
    class Program
    {
        private readonly ILogger logger;
        static InMemoryChannel channel = new InMemoryChannel();

        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.Configure<TelemetryConfiguration>(
                (config) =>
                {
                    config.TelemetryChannel = channel;
                }
            );
            services
                .AddLogging(opt =>
                {
                    opt.AddConsole();
                    opt.AddApplicationInsights();
                })
                .AddTransient<Program>();
            return services.BuildServiceProvider();
        }

        public async Task Run()
        {
            try
            {
                throw new Exception("my exception 111");
            }
            catch (Exception e)
            {
                logger.LogError(e, "Exception occured");
                // How to flush Application insights here
                channel.Flush();

                await Task.Delay(1000);
                throw;
            }
        }
    }
}

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.