Ashkan S
Ashkan S

Reputation: 11471

How to anonymize URL in application insights?

I have URL /users/[email protected]. I don't want to log the actual email in the logs. What can I do?

I really like to be logged like /users/m***[email protected] but I just don't know if it is possible and how.

Update

It is a c# on ASP.net core 3.1 framework.

Upvotes: 0

Views: 375

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

You can use ITelemetryInitializer to modify it.

I write a sample code snippet for asp.net core. For testing purpose, I just replace the information(like the email) in the url. Please feel free to change it to meet your need.

1.Add a new class, and implement it with the following code:

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 the Startup.cs -> ConfigureServices method,register the ITelemetryInitializer:

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

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

Upvotes: 2

Related Questions