Reputation: 1171
I'd like to send some telemetry from within ConfigureServices, for example if I'm missing a config setting I'd like to send an event to AI to track this.
public class Startup
{
public void ConfigureServices( IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
// Is there a way of getting the telemetry client here to send some telemetry?
// e.g. TelemetryClient.TrackEvent("MyEvent");
}
}
Is there any way of getting hold of the telemetry client created by AddApplicationInsightsTelemetry
within ConfigureServices? Is the context even valid at that point?
Upvotes: 1
Views: 670
Reputation: 29985
No, we cannot get hold of the telemetry client created by AddApplicationInsightsTelemetry
within ConfigureServices
before ConfigureServices
method is finished.
You should manually create a telemetry client for your purpose in ConfigureServices
method, like below:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
var s1 = services.AddApplicationInsightsTelemetry(Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]);
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
var telemetryClient = new TelemetryClient(configuration);
telemetryClient.TrackEvent("xxx");
}
Upvotes: 2