add app insights trace logging to .net core console application

为君一笑 提交于 2019-12-04 17:27:06

Actually, faced same problem and finally resolved it looking inside AddApplicationInsights source code. The trick is that you had to register TelemetryClient manually to the container to make this stuff work for console application:

  var telemetryClient = new TelemetryClient(new TelemetryConfiguration()
        {
            InstrumentationKey = config.GetValue("ApplicationInsights:InstrumentationKey")
        });

        services.AddSingleton(x => telemetryClient);

        var provider = services.BuildServiceProvider();

        loggerFactory.AddApplicationInsights(provider, LogLevel.Information);
        var logger = loggerFactory.CreateLogger<Program>();

        logger.LogInformation("Test");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!