Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Want the text log to split folders by month!! #1406

Closed
amwitx opened this issue Feb 27, 2020 · 4 comments
Closed

Want the text log to split folders by month!! #1406

amwitx opened this issue Feb 27, 2020 · 4 comments

Comments

@amwitx
Copy link

amwitx commented Feb 27, 2020

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!!!

@nblumhardt
Copy link
Member

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)

@amwitx
Copy link
Author

amwitx commented Feb 27, 2020

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)

@bartelink
Copy link
Member

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?

@amwitx
Copy link
Author

amwitx commented Feb 27, 2020

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

.WriteTo.Map(
le => new Tuple<DateTime, LogEventLevel>(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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants