在 Application Insights 指标中为每个请求添加自定义属性 (VB.NET)

Adding custom properties for each request in Application Insights metrics (VB.NET)

我正在尝试将自定义属性添加到每个请求的 Application Insights。我正在尝试使用 VB.NET 代码实现此 post (Adding custom properties for each request in Application Insights metrics) 中描述的相同内容。

我将建议的解决方案 C# 代码转换为 VB.NET,如下所示:

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

    }
}

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

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

TelemetryConfiguration.Active.TelemetryInitializers.Add(New CustomTelemetry())

然而,这让我在 Inherits ITelemetryInitializer

上出错

Classes can inherit only from other classes.

我也尝试过使用 "Implements" 而不是继承,但在页面访问时仍然没有自定义属性发送到 AI。

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

我想知道我在这里遗漏了什么?

谢谢

我设法让它工作如下:

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