serilog-sinks-file
serilog-sinks-file copied to clipboard
Rolling file UTC support
Hi. I want to configure serilog-sinks-file to create new log file each day according to UTC.
Please, take a look at your class Clock.cs: https://github.com/serilog/serilog-sinks-file/blob/dev/src/Serilog.Sinks.File/Sinks/File/Clock.cs Here you're using DateTime.Now, but what about using DateTime.UtcNow?
Is there any way to configure it to use UTC? Otherwise can you please add this feature?
Thanks for the suggestion, and sorry about the slow reply. Doing this seems entirely reasonable, but it's on the borderline of being too much of an edge case for us to want to add a setting for it. Forking the sink (it's a very small codebase) could be a decent option at this point - I'll close this issue as I don't think we'll make the change in the near future, but if enough people need the same thing and stumble across this ticket, we can definitely reevaluate. Cheers!
Hello, It sounds strange that the sink doesn't support such a feature. I write my logs in UTC and now, the date in a log file name is very often younger than date of records in the file. Of couse I can use the Reflection, but I guess it is better if this functionality will be implemented out of the box. Thanks.
Hi,
I would also be interested to have such feature out of the box.
Let's give this some more thought 👍
I don't think we'd open up the static hook point as-is (it's only there to facilitate testing); it'd be nicer to get rid of it and integrate through a public clock parameter ... somewhere.
The case against this is the proliferation of arguments to File(), which is a somewhat poor case, but has kept us from being weighed down by too much combinatorial complexity so far :-)
Hi,
How about next to the RollingInterval.Day, we could also use a RollingInterval.UtcDay?
It actually doesn't appear to be such a rare scenario and I'm wondering why this issue is still not solved after five years?!
Anyway, meanwhile we use this HACK as a workaround:
var fileSinkTypes = typeof(Serilog.Sinks.File.FileSink).Assembly.GetTypes();
var clockType = fileSinkTypes.FirstOrDefault(x => string.Equals(x.FullName, "Serilog.Sinks.File.Clock", StringComparison.Ordinal));
var timestampProviderField = clockType?.GetField("_dateTimeNow", BindingFlags.Static | BindingFlags.NonPublic);
timestampProviderField?.SetValue(null, new Func<DateTime>(() => DateTime.UtcNow));
@6bee many other more interesting things got done instead - time and attention is finite unfortunately :-)
https://github.com/serilog/serilog/issues/1920 might evolve into a general solution to this, time providers can select their local time zone, which in this case could be set to UTC.