我正在使用 Application Insights 版本 2.22.0 和 Serilog 5.1.0。我尝试在 HomeController Index 端点中记录一个简单字符串的 1000 次迭代(因此每次都是相同的字符串),但所有这些迭代都会到达 AI。为什么会出现这种情况? AI 不会自动使用自适应采样吗?或者使用 Serilog 覆盖它?如果没有的话如何手动设置?
这是我的代码:
appsettings.json:
"ApplicationInsights": {
"InstrumentationKey": "..."
},
"Serilog": {
"Using": [ "Serilog.Exceptions", "Serilog.Sinks.ApplicationInsights",
"{project namespace}" ],
"Enrich": [ "WithExceptionDetails" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"WebOptimizer": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "logs/log.txt",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "ApplicationInsights",
"Args": {
"connectionString": "InstrumentationKey=...",
"telemetryConverter":
"Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
]
},
程序.cs:
private static WebApplication BuildWebApp(string[] args, IConfigurationQueries configurationQueries, IConfiguration configuration)
{
var builder = WebApplication.CreateBuilder(args);
// Use Castle.Windsor as dependency injection container
var container = new Castle.Windsor.WindsorContainer();
var serviceProviderFactory = new Castle.Windsor.Extensions.DependencyInjection.WindsorServiceProviderFactory(container);
builder.Host.UseServiceProviderFactory(serviceProviderFactory);
builder.Host.ConfigureContainer<Castle.Windsor.IWindsorContainer>(c => Bootstrapper.Install(c));
builder.Host.UseSerilog((context, config) =>
{
config.ReadFrom.Configuration(configuration);
config.Enrich.With(new CustomEnricher(configuration));
});
var startup = new Startup(builder.Configuration, builder.Environment);
startup.ConfigureServices(builder.Services, configurationQueries);
var app = builder.Build();
startup.ConfigurePipeline(app);
return app;
}
Startup.cs:
public void ConfigureServices(IServiceCollection services, IConfigurationQueries configurationQueries)
{
...
services.ConfigureInstrumentation(_configuration, _logger);
...
_logger.LogInformation($"Dependency Injection is Configured for Environment '{_environment.EnvironmentName}'.");
}
仪器.cs:
public static IServiceCollection ConfigureInstrumentation(this IServiceCollection services, IConfiguration configuration, ILogger logger)
{
var initializer = new InstrumentationInitializer(configuration);
var options = initializer.CreateServiceOptions();
services.AddSingleton<ITelemetryInitializer, WebTelemetryInitializer>();
services.AddApplicationInsightsTelemetryProcessor<WebTelemetryFilter>();
logger.LogInformation($"Application Insights was initialized with Instrumentation Key '{options.InstrumentationKey}'");
services.AddApplicationInsightsTelemetry(options);
return services;
}
CreateServiceOptions-方法:
public ApplicationInsightsServiceOptions CreateServiceOptions()
{
var instrumentationKey = _configuration["ApplicationInsights:InstrumentationKey"];
var options = new ApplicationInsightsServiceOptions
{
InstrumentationKey = instrumentationKey
};
#if DEBUG
options.DeveloperMode = true;
options.EnableDebugLogger = true;
#endif
return options;
}
即使默认情况下应启用自适应采样,所有跟踪/信息日志语句都会发送到 AI?或者它是否被 Serilog 以某种方式覆盖?
谁能告诉我需要添加什么才能在我的项目中启用自适应采样?
即使默认情况下应启用自适应采样,所有跟踪/信息日志语句都会发送到 AI?或者它是否被 Serilog 以某种方式覆盖?
我使用自适应采样如下:
using Microsoft.ApplicationInsights.Extensibility;
using Serilog;
var rith = WebApplication.CreateBuilder(args);
rith.Host.UseSerilog((cxt, ri_cf) =>
{
ri_cf.ReadFrom.Configuration(cxt.Configuration)
.WriteTo.Console()
.WriteTo.ApplicationInsights(
new TelemetryConfiguration
{
ConnectionString = cxt.Configuration["ApplicationInsights:ConnectionString"]
},
TelemetryConverter.Traces);
});
rith.Services.AddApplicationInsightsTelemetry(options =>
{
options.EnableAdaptiveSampling = true;
});
有了这个,我就可以使用它,并且我还在应用程序洞察中启用了自适应采样。