Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off Microsoft Logs in AspNetCore 2.2 NLog

The following is my Logs

[2019-05-07 15:55:38.4270][PC-20170829ROEW][Info] my logs recorded

[2019-05-07 15:55:38.4929][PC-20170829ROEW][Info] Route matched with {action = >"Get", controller = "Values"}. Executing action >QunarFlight.Web.Controllers.ValuesController.Get (QunarFlight.Web)

[2019-05-07 15:55:38.5798][PC-20170829ROEW][Info] Executing action method >QunarFlight.Web.Controllers.ValuesController.Get (QunarFlight.Web) - Validation >state: Valid

[2019-05-07 15:55:38.6066][PC-20170829ROEW][Info] Executed action method >QunarFlight.Web.Controllers.ValuesController.Get (QunarFlight.Web), returned >result Microsoft.AspNetCore.Mvc.ObjectResult in 22.6363ms.

[2019-05-07 15:55:38.6066][PC-20170829ROEW][Info] Executing ObjectResult, >writing value of type 'System.String[]'.

I want to close all Microsoft default logs( Last four records ), only keep the logs I have recorded( the first one record). i modify appsettings.json on the following

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft.AspNetCore.Hosting": "Warning",
    "Microsoft.AspNetCore.Routing": "Warning",
    "Microsoft.AspNetCore.Mvc": "Warning"
 }

in this case, my recorded logs will not be output. if i remove "Microsoft.AspNetCore.Mvc": "Warning" my recorded logs will be output .How should I modify it?

appsettings.json

"Logging": {
   "LogLevel": {
     "Default": "Information",
     "Microsoft.AspNetCore.Hosting": "Warning",
     "Microsoft.AspNetCore.Routing": "Warning",
     "Microsoft.AspNetCore.Mvc": "Warning"
}

configure in Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        env.ConfigureNLog("NLog.config");
        loggerFactory.AddNLog();
        app.UseMiddleware<CustomMiddleware>();
        app.UseMvc();
    }

Program.cs

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((builderContext, config) =>
        {
            config.AddJsonFile("appsetting.json", optional: true, reloadOnChange: true)
             .AddJsonFile($"appsettings.{builderContext.HostingEnvironment.EnvironmentName}.json", true, true)
             ;
        })
            .UseStartup<Startup>();
like image 347
Z.Chen Avatar asked Nov 01 '25 00:11

Z.Chen


1 Answers

If you had included ${logger} in your file-Layout in NLog.config, then it would be much easier to help you setup the logger-filter.

But I found this random clue on google:

Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor:Information: Executing ObjectResult

So I guess you need to change your MEL-Config to this:

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft.AspNetCore.Hosting": "Warning",
    "Microsoft.AspNetCore.Infrastructure": "Warning",
    "Microsoft.AspNetCore.Routing": "Warning",
    "Microsoft.AspNetCore.Mvc": "Warning"
 }

One could even consider doing it like this:

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft.AspNetCore": "Warning"
 }

Notice NLog v5 excludes itself from Microsoft LoggerFactory filtering. Instead one can use finalMinLevel= in NLog.config (Can also be configured in appsettings.json):

<nlog throwConfigExceptions="true">
    <targets>
       <target name="logTarget" xsi:type="..." />
    </targets>
    <rules>
      <logger name="System.*" finalMinLevel="Warn" />
      <logger name="Microsoft.*" finalMinLevel="Warn" />
      <logger name="Microsoft.Hosting.Lifetime*" finalMinLevel="Info" />
      <logger name="*" minLevel="Debug" writeTo="logTarget" />
    </rules>
</nlog>
like image 200
Rolf Kristensen Avatar answered Nov 04 '25 01:11

Rolf Kristensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!