Why does RollingInterval.Day keep creating new Serilog Logs files?
Asked Answered
S

2

8

I am using Serilog as a logger for my program. Trying to make Rolling interval files that append the yyyymmdd date to each file.

The code below is exactly what I have used the entire time and it worked for a good 4-5 days of testing and developing my UWP app further. Then I think my app started creating multiple files with the same date titling them logs-yyyymmdd_01, logs-yyyymmdd_02, etc.

This is in the MainPageViewModel Constructor

            Log.Logger = new LoggerConfiguration().MinimumLevel.Debug()
            .WriteTo.File(this.GetFile("logs-"), rollingInterval: RollingInterval.Day)
            .CreateLogger();

        public string GetFile(String FileName)
        {
            string s = ApplicationData.Current.LocalFolder.Path;
            return s + "\\" + FileName;
        }

I have the suspicion that it is calling the constructor multiple times and that might be part of the issue. But the rolling interval should only create a new file when its a new day right? Because when the app is launched multiple times, it used to use whatever file corresponded to todays date. So I either need a way to make the Log.Logger static so that it is universal, or a way to make it use only the one file. Any help would be much appreciated

Seaver answered 12/6, 2019 at 19:43 Comment(1)
pretty sure it should append as you hope - maybe it's hitting a size limit? The code in the serilog.sinks.file repo is pretty clean - recommend figuring it out from there (the tests also specify the various behaviours well)Arlaarlan
S
6

Update:

Since the rolling file is becoming deprecated you can achieve with the Serilog.Sinks.File package in Nuget. The syntax for rolling file is:

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console()
            .WriteTo.File($@"{logPath}\log-.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();

Have you installed the Serilog.Sinks.RollingFile from NuGet? My understanding that the rolling file aspect for Serilog.Sinks.File is still dependent on that dependency.

With that being said I use the following:

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console()
            .WriteTo.RollingFile($@"{Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)}\Organization\{Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName)}\Logs\log.txt")
            .CreateLogger();

That defaults to a daily log file unless the size becomes too large.

Stowers answered 12/6, 2019 at 19:58 Comment(7)
Yeah I had Serilog.Sinks.File already before, it looks like RollingFile adds the syntax you just presented. I tried it out and I am still getting the multiple files created. Could this still happen if my constructor is run multiple times? Is there a better place in my code to put my Logger constructor to make sure it only runs once?Seaver
As BWhelan says @Greg, File's rolling support is ... rolled in and does not rely on .RollingFileArlaarlan
@RubenBartelink ah I see, I read it as Serilog.Sinks.File requiring RollingFile. My badSeaver
@Seaver Well, it should not be in a constructor. You should have that code in your entry point or an abstracted provider that constructs it once.Stowers
@Stowers Thank you, I used the call Hierarchy to figure out where my constructor for my MainViewModel was being called multiple times and that fixed the issue. Thank you for your new way of doing the RollingFile syntax!Seaver
Just a heads-up, RollingFile is the deprecated, older sink; the File sink is the newer one.Obliging
Yeah I just saw that.Stowers
G
4

Had the same issue. Adding the shared parameter in the WriteTo.File() call fixed it for me:

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.File(@"C:\Logs\Log_.txt", shared: true, rollingInterval: RollingInterval.Day)
            .CreateLogger();
Geoffreygeoffry answered 19/6, 2021 at 9:14 Comment(2)
Thanks. This is the real solution. I wonder why the shared parameter is required, when disposing old instances.Suspender
@Suspender per documentation technically the accepted answer is correct. However, “shared” should be added if more than one process touches the log file. The documentation outlines when and why. github.com/serilog/serilog-sinks-fileStowers

© 2022 - 2024 — McMap. All rights reserved.