簡體   English   中英

從 WebForms .NET 框架應用程序中的 ServiceCollection 獲取 App Insights 的 ILogger 實例

[英]Get an ILogger instance for App Insights from ServiceCollection in a WebForms .NET Framework application

我們想要一個 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 實例?

  • 如果您使用的是 visual studio,則可以直接通過connection service選項配置應用程序 Insights。

  • 只需在解決方案資源管理器中單擊co.neted service文件在此處輸入圖像描述

  • 之后單擊Add a service dependency以配置應用程序見解在此處輸入圖像描述

  • 現在將出現一個彈出窗口,其中 select Azure Application Insights然后單擊下一步。在此處輸入圖像描述

  • 現在 select 一個應用程序 Insghts from the list 或創建一個新的。

在此處輸入圖像描述

  • 現在只需繼續單擊下一步/完成,直到該過程完成。 因此,現在您已經在 web 表單中配置了應用程序洞察,跟蹤將開始出現在應用程序洞察中。

Azure傳送門output:-在此處輸入圖像描述

我發現它沒有登錄到 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注入遙測客戶端 package

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM