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

时间:2019-04-05 09:08:53

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

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

在发生异常的情况下,我要将其记录到Application Insights。但是由于Application Insights确实不会立即发送跟踪,所以我想刷新日志。

是否可以在ILogger之后触发应用程序见解的刷新?

        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;
            }
        }

1 个答案:

答案 0 :(得分:1)

请尝试使用方法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;
            }
        }
    }
}

希望有帮助。