从WPF发送至Azure Application Insights的数据中删除RoleInstance。

问题描述 投票:0回答:1

我试图使用这个文档将Application Insights添加到一个WPF应用程序中。https:/docs.microsoft.comen-usazureazure-monitorappwindows-desktop。. 基本的集成是有效的。我现在正试图从提交的数据中删除RoleInstance属性,正如文档中所描述的那样,因为它包含了用户的计算机名称(个人身份信息)。这是我的代码,基于上面的文档。

Telemetry.cs

public static class Telemetry
{
    public static TelemetryClient Client;

    public static void Close()
    {
        if (Client != null)
        {
            Client.Flush();
            System.Threading.Thread.Sleep(1000);
        }
    }

    public static void Initialize()
    {
        TelemetryConfiguration.Active.InstrumentationKey = "xxxxxxxx";
        TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());

        Client = new TelemetryClient(TelemetryConfiguration.Active);

        Client.Context.Session.Id = Guid.NewGuid().ToString();
        Client.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
    }

    private class MyTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName))
            {
                telemetry.Context.Cloud.RoleInstance = null;
            }
        }
    }
}

App.xaml.cs

public partial class App : Application
{
    protected override void OnExit(ExitEventArgs e)
    {
        Telemetry.Close();

        base.OnExit(e);
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        Telemetry.Initialize();
    }
}

当我调用 Telemetry.Client.TrackEvent(), Telemetry.Initialize() 运转,和 RoleInstance 从一开始就是空的。但是,发送给AI的数据中包含了我的电脑名作为RoleInstance。不知道如何进一步调试。

注:文档中描述了集成到WinForms中,而我是在WPF中,所以我猜测使用了 App.OnStartup 而不是 Main.

c# wpf azure-application-insights
1个回答
1
投票

我刚刚发现了一些有趣的事情,如果你为下面的内容设置一个虚值 RoleInstance,它真的生效了。也许会检查nullempty值,并将其替换为真正的。RoleInstance 的后端。作为一个变通方法,你可以指定一个虚值来代替nullempty值。

这是我使用虚值的测试结果。

enter image description here

在azure门户,

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.