如何在 application insights 中匿名化 URL?
How to anonymize URL in application insights?
我有URL/users/myuser@gmail.com
。我不想在日志中记录实际的电子邮件。我能做什么?
我真的很喜欢像 /users/m***r@gmail.com
这样的记录,但我只是不知道这是否可能以及如何实现。
更新
它是 ASP.net 核心 3.1 框架上的 c#。
可以使用ITelemetryInitializer修改
我为 asp.net 核心编写了示例代码片段。出于测试目的,我只是替换了 url 中的信息(如电子邮件)。请随时更改它以满足您的需要。
1.Add一个新的class,用下面的代码实现:
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.in Startup.cs -> ConfigureServices方法,注册ITelemetryInitializer:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddApplicationInsightsTelemetry();
//use this line of code to register.
services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}
我有URL/users/myuser@gmail.com
。我不想在日志中记录实际的电子邮件。我能做什么?
我真的很喜欢像 /users/m***r@gmail.com
这样的记录,但我只是不知道这是否可能以及如何实现。
更新
它是 ASP.net 核心 3.1 框架上的 c#。
可以使用ITelemetryInitializer修改
我为 asp.net 核心编写了示例代码片段。出于测试目的,我只是替换了 url 中的信息(如电子邮件)。请随时更改它以满足您的需要。
1.Add一个新的class,用下面的代码实现:
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.in Startup.cs -> ConfigureServices方法,注册ITelemetryInitializer:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddApplicationInsightsTelemetry();
//use this line of code to register.
services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}