0

I want to log custom properties for both request and page view telemetries in Application Insight. I am working in an asp.net webforms project. The following is my code.

public class CustomTelemetry : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
       
            var requestTelemetry = telemetry as RequestTelemetry;
            var pageViewTelemetry = telemetry as PageViewTelemetry;


            if (requestTelemetry != null)
            {
                SetRequestTelemetry(requestTelemetry);
            }

            if (pageViewTelemetry != null)
            {
                SetPageViewTelemetry(pageViewTelemetry);
            }
        
    }

    private void SetRequestTelemetry(RequestTelemetry requestTelemetry)
    {
        //requestTelemetry.Context.Properties["RequestTelemetryUser"] = 
            //"RequestTelemetryUser";
        //requestTelemetry.Properties.Add("LoggedInUserName", "DummyUser");
        requestTelemetry.Properties["RequestTelemetryUser"] = "RequestTelemetryUser";
    }
    private void SetPageViewTelemetry(PageViewTelemetry pageViewTelemetry)
    {
        pageViewTelemetry.Properties["PageViewTelemetryUser"] = "PageViewTelemetryUser";
    }
}

In the Global.asax I have the following line.

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

While the custom properties are logged for the page view they are not logged for the request. I have commented out other additional code I tried within the SetRequestTelemetry method. One of the suggested solution at Add Custom Properties to Application Insights Request Telemetry in WCF doesn't work as it's not a WCF application.

Massey
  • 1,099
  • 3
  • 24
  • 50
  • Can you inspect the properties when debugging and confirm the code is executed and the properties are set? – Peter Bons Jun 27 '22 at 06:01
  • Does this answer your question? [Add Custom Properties to Application Insights Request Telemetry in WCF](https://stackoverflow.com/questions/61923617/add-custom-properties-to-application-insights-request-telemetry-in-wcf) – Ecstasy Jun 27 '22 at 08:02
  • [Adding custom property to all Application Insight traces](https://stackoverflow.com/questions/52326335/adding-custom-property-to-all-application-insight-traces), [Adding custom properties for each request in Application Insights metrics](https://stackoverflow.com/questions/29036729/adding-custom-properties-for-each-request-in-application-insights-metrics) and [Can't override Success of RequestTelemetry](https://github.com/microsoft/ApplicationInsights-dotnet/issues/2396) – Ecstasy Jun 29 '22 at 03:48

0 Answers0