【问题标题】:How to anonymize URL in application insights?如何匿名化应用程序洞察中的 URL?
【发布时间】:2021-01-13 18:55:16
【问题描述】:

我有网址/users/myuser@gmail.com。我不想在日志中记录实际的电子邮件。我能做什么?

我真的很喜欢像/users/m***r@gmail.com 这样被记录,但我只是不知道这是否可能以及如何。

更新

它是一个基于 ASP.net core 3.1 框架的 c#。

【问题讨论】:

  • 你的项目是什么样的?像 .net core web 还是别的什么?
  • @IvanYang 它在 dotnet core 3.1 上。也更新了问题:+1:
  • 你可以用ITelemetryInitializer试试,看看那里能不能修改url。
  • 我猜可以通过实现自己的ITelemetryProcessor 并使用services.AddApplicationInsightsTelemetryProcessor<YourTelemetryProcessor>() 添加它来实现
  • @AshkanSirous,如果您还有其他问题,请告诉我。如果它有帮助,请接受它作为答案:)。

标签: azure .net-core azure-application-insights appinsights


【解决方案1】:

您可以使用ITelemetryInitializer进行修改。

我为 asp.net core 编写了一个示例代码 sn-p。出于测试目的,我只是替换了 url 中的信息(如电子邮件)。请随时更改它以满足您的需要。

1.添加一个新类,并用如下代码实现:

public class MyTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
       
        if (requestTelemetry == null) return;

        if (requestTelemetry.Url.ToString().Contains("Home"))
        {
            string str1 = requestTelemetry.Url.ToString();

            //for test purpose, it is just replaced here.
            Uri uri = new Uri(requestTelemetry.Url.ToString().Replace("Home", "myhome222"));
            requestTelemetry.Url = uri;

            //it will also be shown in the Name and Operation_name, you should write your logic to do that.
            //requestTelemetry.Name = "";
            //requestTelemetry.Context.Operation.Name = "";
        }         
        
    }
}

2.在Startup.cs -> ConfigureServices方法中,注册ITelemetryInitializer:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddApplicationInsightsTelemetry();

        //use this line of code to register.
        services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2016-09-17
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多