![](/img/trans.png)
[英]Logging to the Application Insights TRACE event type using ILogger in ASP.Net Core
[英]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 或創建一個新的。
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.