7

I use Serilog on a .NET Core application with RollingFile. I wonder, if there are different filename placeholders? I only know {Date}.

For example, I have code like

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.RollingFile("Logs/{Date}.log")
            .CreateLogger();

Are there any other options for the filename like {Date}? I'd like to have log files by hour.

Kim
  • 150
  • 1
  • 2
  • 7

3 Answers3

15

At the time of this writing, Serilog's Rolling File sink supports 3 (three) specifiers:

  • {Date}, which is formatted as yyyyMMdd
  • {Hour}, which is formatted as yyyyMMddHH
  • {HalfHour}, which is formatted as yyyyMMddHHmm

You can see it in the README of the Rolling File sink, as well as in the source code of the Rolling File sink.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
8

UPDATE : 2022

Because "Serilog.Sinks.RollingFile" is obsolete now you should use "Serilog.Sinks.File" It's as easy though :

var log = new LoggerConfiguration()
    .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();

This will append the time period to the filename, creating a file set like:

log20180631.txt
log20180701.txt
log20180702.txt

For differentes choices :

enter image description here

https://github.com/serilog/serilog-sinks-file

Igor Beaufils
  • 848
  • 2
  • 12
  • 29
0

You can also add environment variables into the appsettings.json as %USERNAME%

Ignore
  • 41
  • 5