{"record":{"id":"62c038cedcb9c279","repo":"winsw/winsw","slug":"cannot-roll-the-file-because-matching-pattern-not","errorCode":null,"errorMessage":"Cannot roll the file because matching pattern not found","messagePattern":"Cannot roll the file because matching pattern not found","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/WinSW.Core/LogAppenders.cs","lineNumber":651,"sourceCode":"                            if (lastNumber > nextFileNumber)\n                            {\n                                nextFileNumber = lastNumber;\n                            }\n                        }\n                        else\n                        {\n                            throw new IOException($\"File {f} does not follow the pattern provided\");\n                        }\n                    }\n                    catch (Exception e)\n                    {\n                        throw new IOException($\"Failed to process file {f} due to error {e.Message}\", e);\n                    }\n                }\n\n                if (nextFileNumber == 0)\n                {\n                    throw new IOException(\"Cannot roll the file because matching pattern not found\");\n                }\n\n                nextFileNumber++;\n            }\n\n            return nextFileNumber;\n        }\n    }\n\n    internal sealed class StreamCopyOperation\n    {\n        private const int BufferSize = 1024;\n\n        private readonly byte[] buffer;\n        private readonly Stream reader;\n\n        private int startIndex;\n        private int endIndex;","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/winsw/winsw/blob/1d0ee4a91bad596d5e7e9c360f2b39ef54674674/src/WinSW.Core/LogAppenders.cs#L633-L669","documentation":"Thrown by GetNextFileNumber after all matching files have been processed and nextFileNumber is still 0. This means files were found by the glob, but none yielded a positive sequence number — the appender cannot determine what the next file number should be and refuses to roll.","triggerScenarios":"Directory.GetFiles returns a non-empty array, but every file's extracted 4-character post-# segment parses as 0 (or a non-positive value). When int.TryParse succeeds with value 0, the condition lastNumber > nextFileNumber (0 > 0) is false, so nextFileNumber is never incremented. After the loop, nextFileNumber == 0 triggers the throw. This happens when all matched files carry sequence number #0000 or a non-positive value.","commonSituations":"Log files manually numbered #0000; files created by a tool that starts at zero rather than one; a previous failed rotation that left behind #0000 files.","solutions":["Rename or remove log files with #0000 sequence numbers so the appender can start from #0001","Clear the log directory and let WinSW recreate rotation files from scratch","Manually renumber existing rolled files starting from #0001"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before rotation, check whether any matched file has sequence #0000\nstring glob = $\"{baseFileName}.{now:yyyyMMdd}.#*{ext}\";\nforeach (var f in Directory.GetFiles(baseDirectory, glob))\n{\n    var name = Path.GetFileNameWithoutExtension(f);\n    int hash = name.IndexOf('#');\n    if (hash >= 0 && int.TryParse(name.Substring(hash + 1, 4), out int n) && n <= 0)\n    {\n        Console.Error.WriteLine($\"File {f} has non-positive sequence number #{n:D4}; rotation will fail.\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    int next = GetNextFileNumber(ext, baseDirectory, baseFileName, now);\n}\ncatch (IOException ex) when (ex.Message.Contains(\"matching pattern not found\"))\n{\n    // All matched files had sequence #0000 — renumber or clear them\n    eventLogger.WriteEntry($\"Rotation failed: {ex.Message}. Renumber existing rolled files.\");\n}","preventionTips":["Ensure rolled log files start at #0001, not #0000","If rotation fails, clear the log directory or manually renumber files starting from #0001","Do not manually create #0000 files in the log directory"],"tags":["logging","file-rotation","rolling","io"],"backgroundTag":null,"analyzedSha":"1d0ee4a91bad596d5e7e9c360f2b39ef54674674","analyzedAt":"2026-08-13T15:26:09.332Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}