Hide False MSAL 4.64.0.0 MSAL.NetCore .NET 8.0.8 logging messages

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật