【问题标题】:Get an ILogger instance for App Insights from ServiceCollection in a WebForms .NET Framework application从 WebForms .NET Framework 应用程序中的 ServiceCollection 获取 App Insights 的 ILogger 实例
【发布时间】:2023-02-02 17:46:29
【问题描述】:

我们想要一个 ILogger 实例,以便它可以传递给其他库。 我们在下面进行了尝试,但 ILogger 实例未登录到 Application Insights。它成功登录到事件查看器。

        var serviceCollection = new ServiceCollection();
        serviceCollection.AddLogging(builder => builder
        .AddFilter("Default", LogLevel.Information)
        .AddFilter("Microsoft", LogLevel.Warning)
        .AddFilter("System", LogLevel.Warning)
        .AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; })
        .AddApplicationInsights(telemetry =>
        telemetry.ConnectionString = "my-key",
        options => options = new ApplicationInsightsLoggerOptions()));


        var serviceProvider = serviceCollection.BuildServiceProvider();
        var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
        var logger = loggerFactory.CreateLogger("my-logger");
        logger.LogInformation("Hi");

我们添加了必要的包,即 Microsoft.Extensions.Logging 和 Microsoft.Extensions.Logging.ApplicationInsights

有没有办法,我们可以从 AppInsights 的 ServiceCollection 中获取一个 ILogger 实例?

【问题讨论】:

    标签: asp.net webforms azure-application-insights event-viewer ilogger


    【解决方案1】:
    • 如果您使用的是 visual studio,则可以直接通过 connection service 选项配置应用程序 Insights。

    • 只需在解决方案资源管理器中单击 conneted service 文件

    • 之后点击Add a service dependency配置应用洞察

    • 现在会出现一个弹出窗口,选择Azure Application Insights,然后单击下一步。

    • 现在从列表中选择一个应用程序 Insights 或创建一个新的。

    • 现在继续单击下一步/完成,直到该过程完成。因此,现在您已经在 Web 表单中配置了应用程序洞察,跟踪将开始出现在应用程序洞察中。

    Azure 门户输出:-

    【讨论】:

    • 在这种情况下,我将如何配置自定义日志?因为我想将自定义消息记录到事件查看器和 Application Insights,所以我使用 ILogger。
    【解决方案2】:

    我发现它没有登录到 AppInsights,因为没有正确配置 TelemetryChannel 或 TelemetryClient。

    方法 1 - 使用遥测通道

    using (var channel = new InMemoryChannel()) {
                    var serviceCollection = new ServiceCollection();
                    serviceCollection.Configure<TelemetryConfiguration>(config => config.TelemetryChannel = channel);
                    serviceCollection.AddLogging(builder => builder
                    .AddFilter("Default", LogLevel.Information)
                    .AddFilter("Microsoft", LogLevel.Warning)
                    .AddFilter("System", LogLevel.Warning)
                    .AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; })
                    .AddApplicationInsights(config => config.ConnectionString = "my-key",
                    options => { }));
                    var serviceProvider = serviceCollection.BuildServiceProvider();
                    var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
                    var logger = loggerFactory.CreateLogger("my-logger");
                    logger.LogInformation("my new try");                 channel.Flush();
                    System.Threading.Thread.Sleep(1000);
                }
    

    方法 2 - 使用 Microsoft.ApplicationInsights.WorkerService 包注入遥测客户端

    var serviceCollection = new ServiceCollection();
    
                serviceCollection.AddApplicationInsightsTelemetryWorkerService(options => options.ConnectionString = "my-key");
                serviceCollection.AddLogging(builder => builder
                .AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information)
                .AddFilter("Default", LogLevel.Information)
                .AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; }));
    
                var serviceProvider = serviceCollection.BuildServiceProvider();
                var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
                var telemetryClient = serviceProvider.GetService<TelemetryClient>();
                var logger = loggerFactory.CreateLogger("my-logger");
                logger.LogInformation("my new try");                 
                telemetryClient.Flush();
                System.Threading.Thread.Sleep(1000);
    

    两种方法都可以正常工作;我们使用方法 2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2019-07-14
      • 2021-09-19
      • 2018-06-27
      相关资源
      最近更新 更多