Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core LogLevel not working

I'm having trouble getting the logger to work like i want it to. I've set the loglevel to warning, but the console window is still bloated with info logs.

I've provided some examples below, nothing extra is configured in Startup.cs or Program.cs.

I'm happy to provide more information if needed.

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "ConnectionString"
  },
  "Logging": {
    "IncludeScopes": false, 
    "LogLevel": {
      "Default": "Warning",
      "Microsoft": "Warning"
    }
  }
}

Logging example:

public class DishRepository : IDishRepository
{
    private readonly ApplicationDbContext _context;
    private readonly ILogger<DishRepository> _logger;

    public DishRepository(ApplicationDbContext context, ILogger<DishRepository> logger)
    {
        _context = context;
        _logger = logger;
    }

    public IEnumerable<Dish> GetAll()
    {
        try
        {
            _logger.LogInformation("GetAll was called");

            return _context.Dishes
                .Include(d => d.Category)
                .Include(d => d.DishIngredients)
                .ThenInclude(di => di.Ingredient)
                .Include(d => d.PizzaType).ToList();
        }
        catch (Exception e)
        {
            _logger.LogError($"Failed to get all dishes: {e}");
            return Enumerable.Empty<Dish>();
        }

    }
}

When i run my program via VisualStudio i get this:

dotnet console

--------This Works--------

I found the example below at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x it works, but I don't understand why this works and not the appsettings.json example above.

appsettings.json

  "Logging": {
"IncludeScopes": false,
"Debug": {
  "LogLevel": {
    "Default": "Warning"
  }
},
"Console": {
  "LogLevel": {
    "PizzeriaAngular": "Warning",
    "Microsoft": "Warning",
    "Microsoft.AspNetCore": "Warning",
    "Microsoft.EntityFrameworkCore": "Information" 
  }
},
"LogLevel": {
  "Default": "Debug"
}

}

Program.cs still looks like this:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}
like image 265
Tangooe Avatar asked Dec 03 '17 19:12

Tangooe


People also ask

How do I check logs in NET Core?

NET Core agent logs information to the logs folder within C:\ProgramData\Contrast\dotnet-core\ on Windows or /var/tmp/contrast/dotnet-core/ on Linux, by default. Notes: Depending on the setup of the Windows profile and folder view settings, the ProgramData folder may be hidden.

Does NLog support .NET 6?

The NLog. Web. AspNetCore-package supports the platforms: For ASP.NET Core 6, .


2 Answers

There are two config files appsettings.json and appsettings.Development.json. And system use it in development mode.

like image 128
Nodon Avatar answered Sep 25 '22 10:09

Nodon


This code work for me (NetCore 2.x) in class Startup.cs in method ConfigureServices(IServiceCollection services)

services.AddLogging(builder =>
        {
            builder.SetMinimumLevel(LogLevel.Trace);
            builder.AddFilter("Microsoft", LogLevel.Warning);
            builder.AddFilter("System", LogLevel.Error);
            builder.AddFilter("Engine", LogLevel.Warning);
        });
like image 39
Francesco Paolo Passaro Avatar answered Sep 23 '22 10:09

Francesco Paolo Passaro