Hello!
我一直在尝试建立一个演示,以使用AspNetCore应用程序上的Application Insights跟踪IP地址,我知道要实现这一点,您需要使用TelemetryInitializer,我打算使用随附的TelemetryInitializer. sdk ,但是所有文档都指向常规的ASP.NET
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
// Use this if MyCustomTelemetryInitializer constructor has parameters which are DI injected. services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();
This will ensure the telemetry initializer will be part of the TelemetryConfiguration object.
类似的方法可用于删除所有或特定的TelemetryInitializer.使用以下逻辑 之后后调用 已完成AddApplicationInsightsTelemetry()
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
发布评论