繁体   English   中英

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

[英]Remove RoleInstance from data sent to Azure Application Insights from WPF

我正在尝试使用以下文档将 Application Insights 添加到 WPF 应用程序: https://docs.microsoft.com/en-us/azure/azure-monitor/app/windows-desktop 基本集成正在工作。 我现在正尝试从提交的数据中删除 RoleInstance 属性,如文档中所述,因为它包含用户的计算机名称(个人身份信息)。 这是我的代码,基于上面的文档:

遥测.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从一开始就是 null 。 但是,发送给 AI 的数据包含我的计算机名称作为 RoleInstance。 不知道如何进一步调试。

注意:文档描述了与 WinForms 的集成,我在 WPF,所以我猜想使用App.OnStartup而不是Main

我刚刚发现了一些有趣的东西,如果你为RoleInstance设置一个虚拟值,它真的会生效。 也许 null/empty 值将在后端被真正的RoleInstance检查和替换。 作为一种解决方法,您可以只指定一个虚拟值而不是空值/空值。

这是我的带有虚拟值的测试结果:

在此处输入图像描述

在 azure 门户中:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM