nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
284 stars 56 forks source link

Append: True not working and other questions #24

Closed sebasijan closed 3 years ago

sebasijan commented 3 years ago

Adding the logger config as per documentation


  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Error"
    },
    "File": {
      "Path": "app.log",
      "Append": "True",
      "FileSizeLimitBytes": 3, // use to activate rolling file behaviour
      "MaxRollingFiles": 4     // use to specify max number of log files
    }
  }

And registering in my console app

var logFilePath = configuration.GetSection("Logging");

new ServiceCollection()
            .AddLogging(loggingBuilder => loggingBuilder.AddFile(logFilePath))

Then injecting to my class

public sealed class Application : IApplication
{
    private readonly IRORepository<Grv, int> _grvRORepository;
    private readonly ILogger<Application> _logger;

    public Application(
        IRORepository<Grv, int> grvRORepository,
        ILogger<Application> logger)
    {
        EnsureArg.IsNotNull(grvRORepository, nameof(grvRORepository));
        EnsureArg.IsNotNull(logger, nameof(logger));

        _grvRORepository = grvRORepository;
        _logger = logger;
    }

    public async Task RunAsync(CancellationToken cancellationToken)
    {
        _logger.LogInformation("hello there!");

        var grv = await _grvRORepository.GetByIdAsync(8, cancellationToken);

        _logger.LogError("YOU WOT M8");
    }
}

Generates 4 separate files:

app.log

2021-05-28T23:25:25.3186362+01:00   INFO    [Microsoft.EntityFrameworkCore.Infrastructure]  [Microsoft.EntityFrameworkCore.Infrastructure.ContextInitialized]   Entity Framework Core 3.1.15 initialized 'DomainContext' using provider 'EntityFrameworkCore.Jet' with options: DataAccessProviderFactory 

app1.log

2021-05-28T23:25:25.3284572+01:00   INFO    [Allways.Application.Application]   [0] hello there!

app2.log

2021-05-28T23:25:25.7681790+01:00   INFO    [Microsoft.EntityFrameworkCore.Database.Command]    [Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted]    Executed DbCommand (60ms) [Parameters=[@__id_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT TOP 1 `t`.`GRVNumber`, `t`.`GrvNumber`
FROM `TGRV` AS `t`
WHERE `t`.`GRVNumber` = @__id_0

app3.log

2021-05-28T23:25:25.8888947+01:00   FAIL    [Allways.Application.Application]   [0] YOU WOT M8

These files are overwritten each time I run the application

Is that as expected? Shouldn't my "Append": "True" option cause them to not overwrite previous log files?

Also, how does the log level work, and how do I know what log is going to be written to which file?

The docs, while useful in registering the library, don't really explain how to use it properly

Really appreciate any guidance, thanks

sebasijan commented 3 years ago

I seem to have misunderstood the meaning of this section in the config

  "FileSizeLimitBytes": 3, // use to activate rolling file behaviour
  "MaxRollingFiles": 4     // use to specify max number of log files

After setting both values to 0, it logs everything to a single file and appends it