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
Interviews2 answers
How to restricted logging into Azure Application Insights
2 Answers
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;
public MyTelemetryProcessor(ITelemetryProcessor next){_next = next;}public void Process(ITelemetry item){// Example: filter out requests to /healthif (item is RequestTelemetry request && request.Url.AbsolutePath.Contains("/health")){return; // Don't log this telemetry}_next.Process(item);}
}
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
{
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
});
✅ 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:
"Logging": {"LogLevel": {"Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information"},"ApplicationInsights": {"LogLevel": {"Default": "Warning",}}}
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.