locked
Adding custom properties for each request in Application Insights metrics RRS feed

  • Question

  • User-254703530 posted

    Hi,

    I'm trying to add custom properties to by Application Insights to each request. I'm trying to achieve the same thing as described on this post (https://stackoverflow.com/questions/29036729/adding-custom-properties-for-each-request-in-application-insights-metrics), however using VB.NET code  

    I converted the C# code to VB.NET as follow :

    public class CustomTelemetry : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;
            if (requestTelemetry == null) return;
            requestTelemetry.Properties.Add("LoggedInUserName", "DummyUser");
    
        }
    }

    to 

    Imports Microsoft.ApplicationInsights.Channel
    Imports Microsoft.ApplicationInsights.DataContracts
    Imports Microsoft.ApplicationInsights.Extensibility
    
    Public Class CustomTelemetry
        Inherits ITelemetryInitializer
    
        Public Sub Initialize(ByVal telemetry As ITelemetry)
            Dim requestTelemetry = TryCast(telemetry, RequestTelemetry)
            If requestTelemetry Is Nothing Then Return
            requestTelemetry.Properties.Add("TEST", "TESTERRRRRR")
        End Sub
    
    End Class
    

    However this gives me an error on Inherits ITelemetryInitializer 

    Classes can inherit only from other classes.
    

    I've also tried to use "Implements" instead of inherits, but it always trigger the exception

    Public Class CustomTelemetry
        Implements ITelemetryInitializer
    
        Public Sub Initialize(ByVal telemetry As ITelemetry)
            Dim requestTelemetry = TryCast(telemetry, RequestTelemetry)
            If requestTelemetry Is Nothing Then Return
            requestTelemetry.Properties.Add("TEST", "TESTERRRRRR")
        End Sub
    
        Private Sub ITelemetryInitializer_Initialize(telemetry As ITelemetry) Implements ITelemetryInitializer.Initialize
            Throw New NotImplementedException()
        End Sub
    End Class

    I was wondering how I can fix this line of code so  custom properties to by Application Insights ?

    Thanks
     

    Tuesday, May 19, 2020 1:12 AM

Answers

  • User-254703530 posted

    I managed to solve it as follow :

    Imports Microsoft.ApplicationInsights.Channel
    Imports Microsoft.ApplicationInsights.DataContracts
    Imports Microsoft.ApplicationInsights.Extensibility
    
    Public Class CustomTelemetry
        Implements ITelemetryInitializer
    
        Private Sub ITelemetryInitializer_Initialize(telemetry As ITelemetry) Implements ITelemetryInitializer.Initialize
            Dim requestTelemetry = CType(telemetry, RequestTelemetry)
            If (requestTelemetry Is Nothing) Then
                Return
            End If
    
            requestTelemetry.Properties.Add("TEST", "TESTERRRRRR")
        End Sub
    End Class
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 3, 2020 12:28 AM

All replies

  • User1881638666 posted

    Hi,

    We use TelemetryIntializers to add or modify the properties to telemetries send from the apps.

    https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling

    Hence, you are in the right path.

    Your vb.net code needs some changes.

    Public Class CustomTelemetry
        Implements ITelemetryInitializer
    
        Public Sub Initialize(ByVal telemetry As ITelemetry) Implements ITelemetryInitializer.Initialize
            Dim requestTelemetry = TryCast(telemetry, RequestTelemetry)
            If requestTelemetry Is Nothing Then Return
            requestTelemetry.Properties.Add("TEST", "TESTERRRRRR")
        End Sub
    End Class

    Thanks,

    Wenushka

    Tuesday, June 2, 2020 3:39 PM
  • User-254703530 posted

    I managed to solve it as follow :

    Imports Microsoft.ApplicationInsights.Channel
    Imports Microsoft.ApplicationInsights.DataContracts
    Imports Microsoft.ApplicationInsights.Extensibility
    
    Public Class CustomTelemetry
        Implements ITelemetryInitializer
    
        Private Sub ITelemetryInitializer_Initialize(telemetry As ITelemetry) Implements ITelemetryInitializer.Initialize
            Dim requestTelemetry = CType(telemetry, RequestTelemetry)
            If (requestTelemetry Is Nothing) Then
                Return
            End If
    
            requestTelemetry.Properties.Add("TEST", "TESTERRRRRR")
        End Sub
    End Class
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 3, 2020 12:28 AM