Serilog: Want the text log to split folders by month!!

Created on 27 Feb 2020  ·  4Comments  ·  Source: serilog/serilog

Hi I have a need,

Output file mode want to add dynamic folder。
For example, by month

d:/logs/project-a/2020-01/log20200101.txt
d:/logs/project-a/2020-02/log20200201.txt
d:/logs/project-a/2020-03/log20200301.txt

Can the implementation callback?
such as

.WriteTo.File(() => $"{DateTime.Now.ToString("yyyy-MM")}/Debug-.txt", rollingInterval: RollingInterval.Day)

Because we have our own plan to clean up the logs, we want to split the folders by month

thanks!!!

Most helpful comment

With tuple packaging, that's fine. Thank you!!

.WriteTo.Map(
le => new Tuple(new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day), le.Level),
(key, log) => log.Async(a => a.File(new CompactJsonFormatter(),
Path.Combine(pathPrefix, $"{key.Item1:yyyy-MM}/{key.Item2}-.txt"),
rollingInterval: RollingInterval.Day)),
sinkMapCountLimit: 1)

All 4 comments

Hi! Try installing _Serilog.Sinks.Map_ and using:

.WriteTo.Map(
  le => new DateTime(le.Timestamp,Year, le.Timestamp.Month, le.Timestamp.Day),
  (month, wt) => wt.File($"{month:yyyy-MM}/log-.txt", rollingInterval: RollingInterval.Day),
  sinkMapCountLimit: 1)

OK can be used.

Can I get the log level in the map? In this way, only one configuration is needed. Otherwise, it needs to be written many times

such as

.WriteTo.Map(
le => new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day),
(month, log) => log.Async(a => a.File(new CompactJsonFormatter(),
Path.Combine(pathPrefix, $"{month:yyyy-MM}/{le.Level?}-.txt"),
rollingInterval: RollingInterval.Day)),
sinkMapCountLimit: 1)

How to get {log. Level}? I want to split files by level

Write it now many times, The only difference is the level part:

.WriteTo.Map(
le => new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day),
(month, log) => log.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug)
.WriteTo.Async(a => a.File(new CompactJsonFormatter(),
Path.Combine(pathPrefix, $"{month:yyyy-MM}/Debug-.txt"),
rollingInterval: RollingInterval.Day))),
sinkMapCountLimit: 1)

.WriteTo.Map(
le => new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day),
(month, log) => log.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information)
.WriteTo.Async(a => a.File(new CompactJsonFormatter(),
Path.Combine(pathPrefix, $"{month:yyyy-MM}/Information-.txt"),
rollingInterval: RollingInterval.Day))),
sinkMapCountLimit: 1)

.WriteTo.Map(
le => new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day),
(month, log) => log.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error)
.WriteTo.Async(a => a.File(new CompactJsonFormatter(),
Path.Combine(pathPrefix, $"{month:yyyy-MM}/Error-.txt"),
rollingInterval: RollingInterval.Day))),
sinkMapCountLimit: 1)

le is a LogEvent, which has a level in it (but I know zero about the Map sink so can't say whether you can use it that way). The answer is in the source of the config overloads - a restrictedToMinLevel constraint might make more sense?

With tuple packaging, that's fine. Thank you!!

.WriteTo.Map(
le => new Tuple(new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day), le.Level),
(key, log) => log.Async(a => a.File(new CompactJsonFormatter(),
Path.Combine(pathPrefix, $"{key.Item1:yyyy-MM}/{key.Item2}-.txt"),
rollingInterval: RollingInterval.Day)),
sinkMapCountLimit: 1)

Was this page helpful?
0 / 5 - 0 ratings