2
Reply

How to restricted logging into Azure Application Insights

Utpal Dutta

Utpal Dutta

1y
1k
0
Reply

I have an aplication deployed in azure with application insight with different log levels (Information,Error,Debug).
All types of logs are storing in app insight.
Now my application is stable and dont want to store every logs inside app insight.
How we can do that without changing any code in backend.
Is it possible to restrict this through Application Insight only?
Note: Asuming my deployed configuration level in Information mode. And want to restrict some of the information mode logs through Application insight

    To restrict or control what gets logged into Azure Application Insights, you can use several methods depending on your scenario. Here’s a complete guide for restricting or filtering telemetry being sent to Application Insights in an ASP.NET Core application:

    ✅ 1. Use Telemetry Processor
    A Telemetry Processor allows you to filter out telemetry (like requests, dependencies, traces, exceptions) before it’s sent to Application Insights.

    Example: Custom Telemetry Processor
    csharp
    Copy
    Edit
    public class MyTelemetryProcessor : ITelemetryProcessor
    {
    private ITelemetryProcessor _next;

    1. public MyTelemetryProcessor(ITelemetryProcessor next)
    2. {
    3. _next = next;
    4. }
    5. public void Process(ITelemetry item)
    6. {
    7. // Example: filter out requests to /health
    8. if (item is RequestTelemetry request && request.Url.AbsolutePath.Contains("/health"))
    9. {
    10. return; // Don't log this telemetry
    11. }
    12. _next.Process(item);
    13. }

    }
    Register it in Startup.cs or Program.cs:

    csharp
    Copy
    Edit
    services.AddApplicationInsightsTelemetry();
    services.AddApplicationInsightsTelemetryProcessor();
    ✅ 2. Use Telemetry Initializer
    A Telemetry Initializer adds or modifies data but doesn’t block it. You can use this to suppress sensitive fields or add custom properties (e.g., user ID, tenant ID).

    Example: Mask PII
    csharp
    Copy
    Edit
    public class CustomTelemetryInitializer : ITelemetryInitializer
    {
    public void Initialize(ITelemetry telemetry)
    {
    if (telemetry is RequestTelemetry request)
    {
    // Mask user-agent or add a tag
    request.Context.User.Id = “Anonymous”;
    }
    }
    }
    Register it:

    csharp
    Copy
    Edit
    services.AddSingleton();
    ✅ 3. Control Sampling
    Sampling reduces the volume of telemetry sent to Application Insights, helping with cost and noise.

    Example: Adaptive Sampling (default)
    csharp
    Copy
    Edit
    services.AddApplicationInsightsTelemetry(options =>
    {
    options.EnableAdaptiveSampling = true;
    });
    Example: Fixed-rate Sampling
    csharp
    Copy
    Edit
    services.Configure((config) =>
    {
    var processor = new SamplingTelemetryProcessor
    (
    config.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Build()
    )
    {
    SamplingPercentage = 25.0 // Keep 25% of telemetry
    };
    config.DefaultTelemetrySink.TelemetryProcessorChainBuilder.Use((next) => processor);
    });
    ✅ 4. Disable Logging Completely for Specific Providers
    You can control what logs are forwarded using LoggingBuilder filters.

    csharp
    Copy
    Edit
    services.AddLogging(builder =>
    {
    builder.AddFilter(“”, LogLevel.Warning); // Log warnings and above
    });
    ✅ 5. Disable Automatic Collection
    You can disable certain automatic telemetry types like dependencies, performance counters, etc.

    csharp
    Copy
    Edit
    services.AddApplicationInsightsTelemetry(options =>
    {
    options.EnableDependencyTrackingTelemetryModule = false;
    options.EnableRequestTrackingTelemetryModule = false;
    });
    ✅ 6. Environment-based Filtering
    Only log in production, or restrict based on environment.

    csharp
    Copy
    Edit
    if (env.IsProduction())
    {
    services.AddApplicationInsightsTelemetry();
    }

    It’s possible via configuration (appsettings.json/web.config/whatever you have).
    Here’s an example of such configuration section from appsettings.json:

    1. "Logging": {
    2. "LogLevel": {
    3. "Default": "Information",
    4. "Microsoft": "Warning",
    5. "Microsoft.Hosting.Lifetime": "Information"
    6. },
    7. "ApplicationInsights": {
    8. "LogLevel": {
    9. "Default": "Warning",
    10. }
    11. }
    12. }

    Also, if the intention is to save money by reducing logging you can specify sampling level for Application Insight instance in Azure. Having 10% sampling means only 10% of your logging entries will be sent to Application Insight.