[英]Remove RoleInstance from data sent to Azure Application Insights from WPF
I'm trying to add Application Insights to a WPF app using this documentation: https://docs.microsoft.com/en-us/azure/azure-monitor/app/windows-desktop .我正在尝试使用以下文档将 Application Insights 添加到 WPF 应用程序: https://docs.microsoft.com/en-us/azure/azure-monitor/app/windows-desktop 。 The basic integration is working.
基本集成正在工作。 I am now trying to remove the RoleInstance property from the submitted data, as described in the documentation, as this contains the user's computer name (personally identifiable information).
我现在正尝试从提交的数据中删除 RoleInstance 属性,如文档中所述,因为它包含用户的计算机名称(个人身份信息)。 Here's my code, based on the documentation above:
这是我的代码,基于上面的文档:
Telemetry.cs遥测.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 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();
}
}
When I call Telemetry.Client.TrackEvent()
, Telemetry.Initialize()
runs, and RoleInstance
is null from the start.当我调用
Telemetry.Client.TrackEvent()
时, Telemetry.Initialize()
运行,并且RoleInstance
从一开始就是 null 。 But, the data sent to AI contains my computer name as the RoleInstance.但是,发送给 AI 的数据包含我的计算机名称作为 RoleInstance。 Not sure how to debug further.
不知道如何进一步调试。
Note: the documentation describes integration into WinForms, and I'm in WPF, so I've guessed at using App.OnStartup
instead of Main
.注意:文档描述了与 WinForms 的集成,我在 WPF,所以我猜想使用
App.OnStartup
而不是Main
。
I just found something interesting, if you set a dummy value for RoleInstance
, it really takes effect.我刚刚发现了一些有趣的东西,如果你为
RoleInstance
设置一个虚拟值,它真的会生效。 Maybe the null/empty value will be checked and replaced by the real RoleInstance
in the backend.也许 null/empty 值将在后端被真正的
RoleInstance
检查和替换。 As a workaround, you can just specify a dummy value instead of null/empty value.作为一种解决方法,您可以只指定一个虚拟值而不是空值/空值。
Here is my test result with a dummy value:这是我的带有虚拟值的测试结果:
in azure portal:在 azure 门户中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.