Search code examples
azure-application-insights

Application Insights and .Net Core - 0.0.0.0 IP


All my requests logged on application insights have the 0.0.0.0 IP. Why?

Details:
- Running a app on azure app service
- Using .Net Core 2
- Other info seems ok, like, some requests from around the globe and etc


Solution

  • The IP masking feature of Application Insights can be disabled.

    Know your compliance requirements first before you do so!

    There are two ways to do so:

    First, make a REST call to reconfigure your existing App Insights instance

    I suggest leveraging Azure CLI for that task, as you don't have to take care of the access token. Replace the missing values accordingly

    • <sub-id>
    • <rg-name>
    • <resource location>
    az rest --method patch \
            --url https://management.azure.com/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/microsoft.insights/components/<resource-name>?api-version=2018-05-01-preview \
            --body { \"location\": \"<resource location>\", \"kind\": \"web\", \"properties\": { \"Application_Type\": \"web\", \"DisableIpMasking\": true } }
    
    

    Second, use a custom TelemetryInitializer

    public class CloneIpAddress : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            if (telemetry is ISupportProperties propTelemetry && !propTelemetry.Properties.ContainsKey("client-ip"))
            {
                var clientIPValue = telemetry.Context.Location.Ip;
                propTelemetry.Properties.Add("client-ip", clientIPValue);
            }
        }
    }
    

    And than don't forget to register the type with the DI container

    services.AddSingleton<ITelemetryInitializer, CloneIpAddress>();
    

    The IP address will show up as a custom dimension

    enter image description here