0

project on ASP NET CORE WEB API. dot net version: 6

I decided to use Serilog for logging and I want the logs to be written to a file and also to have the config read from the Json file

Here is what I have:

{
    "Name": "Logger",
    "Args": {
      "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
      "configureLogger": {
        "WriteTo": [
          {
            "Name": "File",
            "Args": {
              "path": "C:\\LogFiles\\{yyyy-MM-dd}\\server.log",
              "rollingInterval": "Day",
              "rollOnFileSizeLimit": true,
              "fileSizeLimitBytes": 100000000,
              "retainedFileCountLimit": null,
              "shared": true,
              "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}|{Level:u3}|{CorrelationId}| {Message:lj}{NewLine}{Exception}"
            }
          }
        ]
      }
    }
  }

Used libraries:

<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Expressions" Version="3.4.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.1-dev-00944" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />

connecting the serilog json file to the project:

 var builder = WebApplication.CreateBuilder(args);
    builder.Configuration.AddJsonFile("serilog.json", true, false);     
    Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .Enrich.FromLogContext()
                .Enrich.WithCorrelationIdHeader(StringConstants.CorrelationId)
                .CreateLogger();
    
   builder.Host.UseSerilog();

Here, as you can see, the path contains {yyyy-MM-dd}, but it doesn't work. 'RollingInterval: Day' is only applied to the file, that is, files are created by date(server20230223.log), and I need files to be created inside a directory that is grouped by date (2023-01-23\server.log)

Is it possible to do this through the config file of the serilog?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249

0 Answers0