TelemetryConfiguration with different Instrumentation Keys
The last few days have involved learning lots of things, breaking lots of things, fixing them and then breaking some other things. I have lived the Facebook "Fail fast" motto!
Anyway, the latest fun and games is using Application Insights, a pretty swish Azure service that collects not only loads of server and performance data from your web apps but also allows you to collect custom metrics. I have a shared library to track these metrics but realised I needed to be able to track events from multiple apps into the same App Insights bucket, while keeping separate buckets for server data - in other words, have more than one instrumentation key.
Initially, I simply passed the instrumentation key I wanted to my library like this:
client = new TelemetryClient(new TelemetryConfiguration(){InstrumentationKey=myParam});
But when called, this produced the rather obtuse error "Telemetry channel should be configured for telemetry configuration before tracking telemetry"
This is another MS classic that makes sense after you've worked out what went wrong rather than showing you what you did.
The Telemetry channel is set up in config so why wasn't it working? Duh, because I had just passed new TelemetryConfiguration() rather than getting TelemetryConfiguration.Active which is what the default constructor uses. I didn't want to use Active however because changing that could affect the config being used by the app and I wanted it just to be used locally. Fortunately, TelemetryConfiguration provides two helper methods: CreateDefault(), which creates a copy of the active configuration and allows you to change what you need to (which I used) and also CreateFromConfiguration(), which does the same thing from a configuration source if you prefer to do something more complex.
Once I created the default one and simply changed the key, hey presto, it all worked.
Easy when you know how!
Anyway, the latest fun and games is using Application Insights, a pretty swish Azure service that collects not only loads of server and performance data from your web apps but also allows you to collect custom metrics. I have a shared library to track these metrics but realised I needed to be able to track events from multiple apps into the same App Insights bucket, while keeping separate buckets for server data - in other words, have more than one instrumentation key.
Initially, I simply passed the instrumentation key I wanted to my library like this:
client = new TelemetryClient(new TelemetryConfiguration(){InstrumentationKey=myParam});
But when called, this produced the rather obtuse error "Telemetry channel should be configured for telemetry configuration before tracking telemetry"
This is another MS classic that makes sense after you've worked out what went wrong rather than showing you what you did.
The Telemetry channel is set up in config so why wasn't it working? Duh, because I had just passed new TelemetryConfiguration() rather than getting TelemetryConfiguration.Active which is what the default constructor uses. I didn't want to use Active however because changing that could affect the config being used by the app and I wanted it just to be used locally. Fortunately, TelemetryConfiguration provides two helper methods: CreateDefault(), which creates a copy of the active configuration and allows you to change what you need to (which I used) and also CreateFromConfiguration(), which does the same thing from a configuration source if you prefer to do something more complex.
Once I created the default one and simply changed the key, hey presto, it all worked.
Easy when you know how!