为了让 Application Insights 捕获信息级别的日志,我们需要添加一个配置设置。以下是 C# 示例:
var config = TelemetryConfiguration.Active;
config.TelemetryChannel.DeveloperMode = true;
config.InstrumentationKey = "Your Instrumentation Key";
config.AddApplicationInsightsLogging();
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddApplicationInsights(config, LogLevel.Information);
ILogger logger = loggerFactory.CreateLogger("Application Insights Logger");
logger.LogInformation("This is an information message.");
我们使用 AddApplicationInsightsLogging() 方法将 ApplicationInsightsOptions 转换为 ILoggerProvider,并将其添加到 LoggerFactory。然后我们可以使用 ILoggerFactory 创建一个 ILogger 并调用 LogInformation() 方法将日志信息记录在 Application Insights 中。最后,请确保 Application Insights 的开发者模式已启用。