0
votes

I'm trying to get Application Insights to work with my WPF application, but whenever I try to call any of the Track functions, I get a nullreferenceexception with this stacktrace

at Microsoft.ApplicationInsights.TelemetryClient.Track(ITelemetry telemetry)

at Microsoft.ApplicationInsights.TelemetryClient.TrackTrace(TraceTelemetry telemetry)

at Microsoft.ApplicationInsights.TelemetryClient.TrackTrace(String message)

I added an application in Azure so that I got an instrumentationKey (hidden in the code below), added the nuget package and entered this code:

var config = new TelemetryConfiguration();
client = new TelemetryClient(config);
config.InstrumentationKey = "myKey";
client.InstrumentationKey = "myKey";
client.TrackTrace("testing testing");
client.Flush();

The crash occurs at the 5:th line above, and It occurs no matter which version of the nuget package I use.

I've heard some people mention an ApplicationInsights.config, but no such file has been generated for me.

2

2 Answers

1
votes

Use TelemetryConfiguration.Active instead of create a new instance.

No need to set InstrumentationKey on client instance. When you set the Active configuration, it will used that key for every new client instance. Only explicit set the key on the telemetry client if you're sending that specific telemetry to a custom / different key than the configuration.

TelemetryConfiguration.Active.InstrumentationKey = "myKey";
client = new TelemetryClient();
client.TrackTrace("testing testing");
client.Flush();
0
votes

Just ran into the same issue. We got it working with code like:

// setup the client
TelemetryClient tc = new TelemetryClient();
tc.InstrumentationKey = "key copied from portal";

// Set session data:
tc.Context.User.Id = Environment.UserName;
tc.Context.Session.Id = Guid.NewGuid().ToString();
tc.Context.Device.OperatingSystem = Environment.OSVersion.ToString();

tc.TrackTrace("some data....");
tc.Flush(); // only for desktop apps

You can see more details at Application Insights on Windows Desktop apps, services and worker roles