簡體   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