Search code examples
c#azureowinazure-application-insights

Problems adding insights to Owin based project


I have been trying to add Azure application insights to a few projects. The whole experience was seamless with a .net core app. However, when I tried to update the Cloud role name property, that is where I could not find a lot for an OWIN based app. I want the name of the bubble in Insights Application Map to appear what I set in this property (My API for example) but it keeps resorting to the resource name that I have for this resource in Azure (my-azure-api). After scouring through most online resources, I was able to do the following which does not work.

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

namespace MyApp.Insights
{
    public class RoleNameInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            // set role name correctly here.
            telemetry.Context.Cloud.RoleName = "My API";            
        }
    }
}

Also added the following in the applicationinsights.config

<Add Type="MyApp.Insights.RoleNameInitializer, MyApp"/>

Added the following to the startup class too (Just as a precaution)

using IntegratedTeleHealthPlatformApi.Insights;
using Microsoft.ApplicationInsights.Extensibility;
using Owin;

namespace MyApp
{
    public partial class Startup
    {

        public void Configuration(IAppBuilder app)
            {

            TelemetryConfiguration
                .Active
                .TelemetryInitializers
                .Add(new RoleNameInitializer());



                ConfigureAuth(app);
                ApplyDatabaseMigrations();       

            }
    }

}

Solution

  • I just setup a simple owin based asp.net project(asp.net web application, then in nuget install Microsoft.Owin.Host.SystemWeb).

    After the setup, in visual studio -> Project -> Add Application Insights Telemetry: enter image description here

    My custom TelemetryInitializer as below: enter image description here

    Then just add the initializer to the applicationinsights.config: enter image description here

    And after execution, the role name is the one which I set in the initializer: enter image description here

    Please have a try if it's ok at your side. And to make sure your RoleNameInitializer is called, you can set breakpoint there to see if it's called or not.