Search code examples
c#xamluwpserilog

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


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


Solution

  • 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.