serilog / serilog-sinks-file

Write Serilog events to files in text and JSON formats, optionally rolling on time or size
Apache License 2.0
334 stars 118 forks source link

LogFileName with Hour/Minute/Seconds in Appsettings #242

Closed xorrot16 closed 2 years ago

xorrot16 commented 3 years ago

add key="serilog:using:File" value="Serilog.Sinks.File" / add key="serilog:write-to:File.path" value="logfile.txt" / add key="serilog:write-to:File.shared" value="true" / add key="serilog:write-to:File.restrictedToMinimumLevel" value="Debug" / add key="serilog:write-to:File.retainedFileCountLimit" value="32" / add key="serilog:write-to:File.rollingInterval" value="Minute" /

with this that is half of the job done.. only seconds are missing!

it will result in a filename logfile202109241208.txt but I need to have logfile2021092412082234.txt to get it unique

I know that I can do it programaticcally by logger.WriteTo.File($"log{StringGenerator.genFileNameFromTimeStampYMDHMS()}.txt", rollingInterval: RollingInterval.Infinite, outputTemplate: .........................

but than there is no Appsettings file support!

I ask for this because my App can be launchend parallel multiple Times so I want for each Time a new LogFile

or is there any way to just change the Filename at runtime while keeping all Options from the Appsettings?

ok what I did to dynamically change the filename is: creating an Enricher this is sufficent for me using Sinks.Map give better control at designtime

like that one: https://stackoverflow.com/questions/61889762/in-serilog-dynamically-changing-log-file-path

but it would be great to have better control like just doing it inhouse from Serilog

xorrot16 commented 3 years ago

ok unfortunally that mapping does not work properly I have two files and one is cut and does not have everything..

I switched to programatically giving the filename without rolling.. but it would be nice to have that ability to append the seconds

nblumhardt commented 2 years ago

Thanks for the note; currently this one is a bit of an edge case and unlikely to receive direct support. Generating the filename you want in code will be the best workaround, though you might also consider writing a custom wrapper sink, or enabling the shared option here and including the process id or another unique value in a single log file. HTH!