In a .NET 8 API project, I’m logging via Serilog to a Application Insights. With every request, I see a lot of False MSAL 4.64.0.0 MSAL.NetCore .NET 8.0.8 messages which I want to hide in traces.
In my service, I’m using ITokenAcquisition to gather a token for the backend service.
accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(new[] { _configuration["PasApi:ScopeForAccessToken"] });
In appsettings.json, I’ve added Logging configuration to exclude specific logging from packages. Also tried with Microsoft.Identity.
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"MSAL.NetCore": "Warning"
}
}
Also tried to add a exclude filtering in the serilog configuration, without any success.
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"Microsoft.AspNetCore": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "ApplicationInsights",
"Args": {
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "Contains(@MessageTemplate, 'False MSAL')"
}
}
]
},
How can I avoid this?
Example MessageTemplate
False MSAL 4.64.0.0 MSAL.NetCore .NET 8.0.8 Microsoft Windows 10.0.19045 [2024-09-17 09:57:46Z – 4671fb35-5f88-41a6-95f0-6b53f07cf66f] TokenEndpoint: ****
Program.cs
builder.Host.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration));
builder.Services.AddAadRegistration(Configuration);
public static IServiceCollection AddAadRegistration(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(configuration.GetSection(AppSettings.AZUREAD))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.TokenValidationParameters.NameClaimType = @"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress";
});
var origins = new List<string>();
configuration.Bind(AppSettings.CORS_MAIN, origins);
services.AddCors(options =>
{
options.AddPolicy(name: Policies.CORS_MAIN,
builder => builder.WithOrigins(origins.ToArray())
.AllowAnyMethod()
.AllowAnyHeader());
});
return services;
}
Packages
Azure.Identity v1.12.0
Microsoft.AspNetCore.Authentication.JwtBearer v8.0.8
Microsoft.Identity.Client 4.64.0
Microsoft.Identity.Web 3.1.0
Serilog.AspNetCore 8.0.2
Serilog.Sinks.ApplicationInsights 4.0.0
11
I created a sample ASP .NET 8 API application, successfully configured Application Insights without any issues.
I’ve installed the below packages in my application
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights
Microsoft.ApplicationInsights.AspNetCore
Microsoft.Identity.Client
my appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"MSAL.NetCore": "Warning"
}
},
"ApplicationInsights": {
"ConnectionString": "<your-connectionstring>"
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"Microsoft.AspNetCore": "Warning",
"MSAL.NetCore": "Warning"
}
},
"WriteTo": [
{
"Name": "ApplicationInsights",
"Args": {
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
},
{
"Name": "Console"
}
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "Contains(@MessageTemplate, 'False MSAL')"
}
}
]
},
"AzureAd": {
"ClientId": "<client-id>",
"ClientSecret": "<client-secret>",
"Instance": "https://login.microsoftonline.com/",
"TenantId": "<tenant-id>"
}
}
I’ve reffered this Github doc and included the below code in my program.cs
for Serilog logging.
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("MSAL.NetCore", LogEventLevel.Warning)
.WriteTo.Console()
.WriteTo.ApplicationInsights(new TelemetryConfiguration { ConnectionString = connectionString }, TelemetryConverter.Traces)
.Filter.ByExcluding(e => e.MessageTemplate.Text.Contains("MSAL"))
.CreateLogger();
program.cs :
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Client;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Serilog;
using Serilog.Events;
var builder = WebApplication.CreateBuilder(args);
string connectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
builder.Services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = connectionString;
});
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("MSAL.NetCore", LogEventLevel.Warning)
.WriteTo.Console()
.WriteTo.ApplicationInsights(new TelemetryConfiguration { ConnectionString = connectionString }, TelemetryConverter.Traces)
.Filter.ByExcluding(e => e.MessageTemplate.Text.Contains(" Final MSAL"))
.CreateLogger();
builder.Host.UseSerilog();
builder.Services.AddSingleton<IConfidentialClientApplication>(sp =>
{
return ConfidentialClientApplicationBuilder.Create(builder.Configuration["AzureAd:ClientId"]) .WithClientSecret(builder.Configuration["AzureAd:ClientSecret"])
.WithAuthority(new Uri($"{builder.Configuration["AzureAd:Instance"]}{builder.Configuration["AzureAd:TenantId"]}")) .WithLogging((Microsoft.Identity.Client.LogLevel level, string message, bool containsPii) =>
{
if (!containsPii && level >= Microsoft.Identity.Client.LogLevel.Warning)
{
Log.Information(message);
}
}, Microsoft.Identity.Client.LogLevel.Warning, enablePiiLogging: false)
.Build();
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Here’s the Output:
Refer this SO answer by @ Harshitha.
5