ClientIpHeaderTelemetryInitializer和AspNetCore [英] ClientIpHeaderTelemetryInitializer and AspNetCore
问题描述
Hello!
我一直在尝试建立一个演示,以使用AspNetCore应用程序上的Application Insights跟踪IP地址,我知道要实现这一点,您需要使用TelemetryInitializer,我打算使用随附的TelemetryInitializer.
I have been trying to setup a demo to track IP addresses with Application Insights on an AspNetCore application, I know in order to achieve this you need to use a TelemetryInitializer I was intending to use the one included on the sdk, however all the documentation points to regular ASP.NET
能否请您提供一些有关如何使用AspNetCore启用这些初始化程序的指南?
Could you please provide some guidance on how these Initializers are enabled using AspNetCore?
谢谢!
推荐答案
由于DI,ASP.NET Core上的配置会有所不同,您可以找到例子.
The configuration on ASP.NET Core will be different due to DI, you can find examples here.
相关部分:
创建新的初始化器.
在
在Startup类的ConfigureServices中调用AddApplicationInsightsTelemetry()
.例如:
Register it into DI Containers before
AddApplicationInsightsTelemetry()
is called in the ConfigureServices of your Startup class. For eg:
// Use this if MyCustomTelemetryInitializer can be constructed without DI injected parameters
services.AddSingleton<ITelemetryInitializer>(new MyCustomTelemetryInitializer());
OR
OR
// Use this if MyCustomTelemetryInitializer constructor has parameters which are DI injected.
services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();
这将确保遥测初始化程序将成为TelemetryConfiguration对象的一部分.
This will ensure the telemetry initializer will be part of the TelemetryConfiguration object.
类似的方法可用于删除所有或特定的TelemetryInitializer.使用以下逻辑
之后后调用
Similar approach can be used to remove all or specific TelemetryInitializers. Use the following logic
after the call to
AddApplicationInsightsTelemetry()
is made
public void ConfigureServices(IServiceCollection services)
{
..
services.AddApplicationInsightsTelemetry("ikey");
// Remove a specific built-in TelemetryInitializer
var tiToRemove = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(AspNetCoreEnvironmentTelemetryInitializer));
if (tiToRemove != null)
{
services.Remove(tiToRemove);
}
// or Remove all
services.RemoveAll(typeof(ITelemetryInitializer)); // this requires importing namespace using Microsoft.Extensions.DependencyInjection.Extensions;
..
}
这篇关于ClientIpHeaderTelemetryInitializer和AspNetCore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!