3
votes

I am trying to set up and use application insights in a class library project. I would like to use it in offline mode while debugging in visual studio.

After following a few guides I have this:

(In the constructor for Engine.cs - the 'Main' class for my lib)

_telemetryClient = new TelemetryClient();

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

Then in the main method for the class:

var metrics = new Dictionary<string, double>();
var properties = new Dictionary<string, string>();
try
{
    // Do stuff and track metrics code...

    telemetryClient?.TrackEvent("Some Event", properties, metrics);
    _telemetryClient?.Flush();
    System.Threading.Thread.Sleep(1000);
}
catch (Exception exception)
{
    _telemetryClient?.TrackException(exception, properties, metrics);
    _telemetryClient?.Flush();
    throw;
}

Since I wish the logging to be configured (eg Azure keys etc) in the calling code consuming the lbrary this project has no other configuration and no applicationinsights.config.

When I debug this in VS however, after choosing Select Application Insights Resource' -> last debug session, the 'Application Insights Search has no data.

1
Sorry if this sounds off but correct me if I'm wrong. I thought application insights was created as a means to determine how you system is doing under load (i.e. how many people are accessing parts of your system at once). Using Application Insights would give you that only you are accessing it. What data are expecting when doing a local only library using this system? Maybe use the in-built profile system instead?D Hansen
this is a completely valid usage of appinsights. it also handles usage (custom events), exception, etc.John Gardner

1 Answers

4
votes

In order for VS to know that your app is using application insights and for the debugger to watch for AI data, you need to have an applicationinsights.config file (even if it is basically empty with no ikey in it) inside the project that is the startup project.

When the debugger starts, we look to see if the kind of debugger starting is one we recognize, and if any of the startup projects have AI config. If we don't detect AI, the AI services stop watching the debugger to prevent it from slowing things down for no reason in projects without AI.

So just add an ApplicationInsights.config file to the startup project that is this contents:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
    <!-- this file should NOT be set to copy output, it is here to allow the AI tools in Visual Studio to watch this project when debugging -->
</ApplicationInsights>

And set the file to "do not copy" in the project settings. it just needs to exist in the startup project, not be in the project output. Because the file is not copied to the output directory, the AI sdk does not load the file, and whatever settings you used to configure the TelemetryClient in code are used.

Also, if you are only using AI for the debug time stuff, i'm pretty sure you don't need the Flush call, I believe in debug mode the output we look for is written as the telemetry is logged, not when the flush call occurs.

If the above is working, when you debug, the Application Insights toolbar button should show you the number of events it has seen go by as well, even though the debug search only shows the last 250 events.