Skip to content

Issue with using local time in the fileNameSelector in RollingFileStream #222

@hansenwang100

Description

@hansenwang100

Currently in the ReadMe, this is the example for using RollingFileLoggerProvider

    .AddZLoggerRollingFile(options =>
    {
        // File name determined by parameters to be rotated
        options.FilePathSelector = (timestamp, sequenceNumber) => $"logs/{timestamp.ToLocalTime():yyyy-MM-dd}_{sequenceNumber:000}.log";
        
        // The period of time for which you want to rotate files at time intervals.
        options.RollingInterval = RollingInterval.Day;
        
        // Limit of size if you want to rotate by file size. (KB)
        options.RollingSizeKB = 1024;        
    })    

However, the rolling interval may never actually take effect because in RollingFileStream.TryChangeNewRollingFile, UTC now is used to check if the next file needs to be created. At UTC midnight, the local time may still be the same day and so the file path selector generates a file with the same name as the current one, and so we will continue writing to the same file unless the process is restarted. I think this can be fixed by using local time when deciding whether or not to roll to a new file, which is consistent with LogInfo using the timestamp's local time.

proposed change: #221

Please let me know what you think, thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions