Skip to content

fix: resolve legacy code-page encodings without opening Preferences f… - #677

Merged
Hirogen merged 2 commits into
Developmentfrom
fix/configured-encoding-registry
Jul 28, 2026
Merged

fix: resolve legacy code-page encodings without opening Preferences f…#677
Hirogen merged 2 commits into
Developmentfrom
fix/configured-encoding-registry

Conversation

@Hirogen

Copy link
Copy Markdown
Collaborator

fix: resolve legacy code-page encodings without opening Preferences first

.NET does not ship the legacy Windows code pages; Encoding.GetEncoding throws for them until CodePagesEncodingProvider is registered. Registration lived only in the SettingsDialog constructor, and every site that resolves an encoding name swallows the ArgumentException and falls back to Encoding.Default. So a user who picked Windows-1252 in Preferences had that choice silently discarded on every restart in which they did not reopen Preferences. Same for a code page persisted per file in a .lxp, and for the settings JSON.

Add EncodingRegistry (LogExpert.Core/Helpers) and route all four resolve sites through it: FileOperationService.FillDefaultEncodingFromSettings, EncodingJsonConverter.ReadJson, PersisterXML.ReadEncoding and the Preferences dropdown. Every method registers the provider before it resolves, so correctness does not depend on one entry point having run first — Program.cs is untouched. Registration uses Lazy with ExecutionAndPublication because files load under Task.Run: the flag must not be observable before registration has completed, or a concurrent first resolve hits the original bug.

Also add Windows-1250 to the Preferences encoding dropdown, and extract SettingsDialog.GetAvailableEncodings so the offered set is assertable without building the dialog.

DetermineEncoding is deliberately unchanged. The precedence chain (explicit encoding, then BOM, then Preferences default, then machine default) is the intended behaviour; it gains regression tests, not edits. PositionAwareReader is not touched.

Delete the memory-mapped read path

MemoryMappedFileReader and LineOffsetIndex were unreachable. LogfileReader sets IsMultiFile = multiFile || fileNames.Length == 1, so a single file makes IsMultiFile true and the if (!IsMultiFile && ...) guard never fires; the multi-file ctor passes multiFile: true. _mmfReader was always null, so BuildIndex, ExtendIndex, GetLine and the fast path in GetLogLineMemoryInternal never ran. Deleting them changes no reachable behaviour.

It also could not simply be switched on. MemoryMappedFile.CreateFromFile opens with FileShare.Read, so while a view is mapped the process producing the log cannot append — a test appending to a mapped file fails with IOException even when the writer requests FileShare.ReadWrite. A read path that locks out the writer is incompatible with tail mode, so fixing IsMultiFile would not have produced a working memory-mapped reader but a regression. Same rationale as ADR 0006: the reader folder holds only code something can reach.

IsMultiFile itself is left exactly as it is; changing it moves every single-file open onto a different name-resolution path and belongs in its own change.

Tests: the four resolve sites are pinned at the site, not just on the helper (FileOperationServiceTests, EncodingJsonConverterTests, PersisterXmlEncodingTests, SettingsDialogEncodingListTests), plus the full DetermineEncoding precedence chain in LogfileReaderEncodingTests.

Supersedes #673 and #671.

…irst
.NET does not ship the legacy Windows code pages; Encoding.GetEncoding throws
for them until CodePagesEncodingProvider is registered. Registration lived only
in the SettingsDialog constructor, and every site that resolves an encoding
*name* swallows the ArgumentException and falls back to Encoding.Default. So a
user who picked Windows-1252 in Preferences had that choice silently discarded
on every restart in which they did not reopen Preferences. Same for a code page
persisted per file in a .lxp, and for the settings JSON.
Add EncodingRegistry (LogExpert.Core/Helpers) and route all four resolve sites
through it: FileOperationService.FillDefaultEncodingFromSettings,
EncodingJsonConverter.ReadJson, PersisterXML.ReadEncoding and the Preferences
dropdown. Every method registers the provider before it resolves, so
correctness does not depend on one entry point having run first — Program.cs is
untouched. Registration uses Lazy with ExecutionAndPublication because files
load under Task.Run: the flag must not be observable before registration has
completed, or a concurrent first resolve hits the original bug.
Also add Windows-1250 to the Preferences encoding dropdown, and extract
SettingsDialog.GetAvailableEncodings so the offered set is assertable without
building the dialog.
DetermineEncoding is deliberately unchanged. The precedence chain (explicit
encoding, then BOM, then Preferences default, then machine default) is the
intended behaviour; it gains regression tests, not edits. PositionAwareReader
is not touched.
Delete the memory-mapped read path
----------------------------------
MemoryMappedFileReader and LineOffsetIndex were unreachable. LogfileReader sets
IsMultiFile = multiFile || fileNames.Length == 1, so a single file makes
IsMultiFile true and the `if (!IsMultiFile && ...)` guard never fires; the
multi-file ctor passes multiFile: true. _mmfReader was always null, so
BuildIndex, ExtendIndex, GetLine and the fast path in GetLogLineMemoryInternal
never ran. Deleting them changes no reachable behaviour.
It also could not simply be switched on. MemoryMappedFile.CreateFromFile opens
with FileShare.Read, so while a view is mapped the process producing the log
cannot append — a test appending to a mapped file fails with IOException even
when the writer requests FileShare.ReadWrite. A read path that locks out the
writer is incompatible with tail mode, so fixing IsMultiFile would not have
produced a working memory-mapped reader but a regression. Same rationale as
ADR 0006: the reader folder holds only code something can reach.
IsMultiFile itself is left exactly as it is; changing it moves every
single-file open onto a different name-resolution path and belongs in its own
change.
Tests: the four resolve sites are pinned at the site, not just on the helper
(FileOperationServiceTests, EncodingJsonConverterTests,
PersisterXmlEncodingTests, SettingsDialogEncodingListTests), plus the full
DetermineEncoding precedence chain in LogfileReaderEncodingTests.
Supersedes #673 and #671.
Comment threadsrc/LogExpert.Tests/Encodings/PersisterXmlEncodingTests.cs Fixed
@Hirogen
Hirogen merged commit fcf7c07 into DevelopmentJul 28, 2026
3 checks passed
@Hirogen
Hirogen deleted the fix/configured-encoding-registry branch July 28, 2026 17:50
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@Hirogen