Skip to content

Adopt injectable Microsoft.Extensions.Logging logging model#381

Merged
ptr727 merged 4 commits into
developfrom
feature/logging-abstraction
Jul 9, 2026
Merged

Adopt injectable Microsoft.Extensions.Logging logging model#381
ptr727 merged 4 commits into
developfrom
feature/logging-abstraction

Conversation

@ptr727

@ptr727 ptr727 commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Adopts the backend-agnostic logging model used by the LanguageTags project, replacing the Serilog-typed LogOptions.Logger with an injectable Microsoft.Extensions.Logging factory.

Library

  • LogOptions now exposes a thread-safe, injectable ILoggerFactory via SetFactory / TrySetFactory, falling back to NullLogger. The library depends only on Microsoft.Extensions.Logging.Abstractions; the Serilog dependency and its IL3058 AOT suppression are gone.
  • FileEx and Download resolve per-class cached loggers through LogOptions.CreateLogger and emit source-generated [LoggerMessage] messages (keeps the build clean under AnalysisMode=All + TreatWarningsAsErrors).
  • LogAndHandle / LogAndPropagate moved onto Microsoft.Extensions.Logging.ILogger as internal helpers (exposed to tests via InternalsVisibleTo).
  • The public Extensions class is renamed to CompressExtensions, resolving the naming clash with the Microsoft.Extensions namespace.

Sandbox

  • Configures a Serilog console logger and injects it through a SerilogLoggerFactory, borrowing the pattern from LanguageTagsCreate.

Tests

  • Migrated the whole test suite from xUnit asserts to AwesomeAssertions fluent assertions, and added LogOptionsTests.

Breaking changes

  • LogOptions.Logger is removed — configure logging with LogOptions.SetFactory(ILoggerFactory).
  • The Extensions class is renamed to CompressExtensions (instance-style extension calls such as value.Compress() are unaffected).

Shipped as the next minor version (3.7); README and HISTORY updated.

Verification

  • Clean build (0 warnings/0 errors); CSharpier and dotnet format style pass.
  • Tests pass except the pre-existing FormatTests.BytestoKilo/BytestoKibi number-formatting failures (present on the clean tree, unrelated to this change).
  • Sandbox prints the themed console line end-to-end, confirming the factory wiring.

🤖 Generated with Claude Code

Replace the Serilog-typed LogOptions.Logger with a backend-agnostic,
injectable ILoggerFactory (LogOptions.SetFactory / TrySetFactory), so
consumers can plug in any logging backend. This mirrors the LanguageTags
project and removes the library's Serilog dependency; the library now
depends only on Microsoft.Extensions.Logging.Abstractions.

- FileEx and Download resolve per-class cached loggers via
  LogOptions.CreateLogger and emit source-generated [LoggerMessage]
  messages, keeping the build clean under AnalysisMode=All.
- Move LogAndHandle / LogAndPropagate onto ILogger as internal helpers.
- Rename the public Extensions class to CompressExtensions to resolve
  the clash with the Microsoft.Extensions namespace.
- Wire a Serilog console logger into the Sandbox via SerilogLoggerFactory.
- Migrate the test suite from xUnit asserts to AwesomeAssertions.

Removing LogOptions.Logger and renaming Extensions are breaking API
changes, shipped as the next minor version (3.7).
Copilot AI review requested due to automatic review settings July 9, 2026 23:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request migrates the library’s logging from a Serilog-coupled global logger to an injectable Microsoft.Extensions.Logging model, making the core library backend-agnostic while keeping the Sandbox as a Serilog-based wiring example.

Changes:

  • Replaced LogOptions.Logger with a thread-safe, injectable ILoggerFactory model and updated FileEx/Download to log through cached ILogger instances with [LoggerMessage] source-generated calls.
  • Renamed the public compression extensions type to CompressExtensions and moved logger helpers onto Microsoft.Extensions.Logging.ILogger as internal extensions (exposed to tests via InternalsVisibleTo).
  • Migrated tests to AwesomeAssertions and added coverage for LogOptions.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
version.json Bumps the minor version for the logging model change.
Directory.Packages.props Adds central versions for AwesomeAssertions and logging abstractions, plus sandbox logging bridge.
Utilities/Utilities.csproj Drops Serilog dependency, adds Microsoft.Extensions.Logging.Abstractions, and exposes internals to tests.
Utilities/LogOptions.cs Implements global ILoggerFactory configuration and logger creation logic.
Utilities/FileEx.cs Switches retry/error logging to Microsoft.Extensions.Logging via cached ILogger.
Utilities/Download.cs Switches exception logging to Microsoft.Extensions.Logging via cached ILogger.
Utilities/Extensions.cs Renames compression extensions type and introduces internal logger helper extensions + [LoggerMessage] methods.
Sandbox/Sandbox.csproj Sets RootNamespace and adds Serilog.Extensions.Logging for factory injection.
Sandbox/Program.cs Configures Serilog and injects an ILoggerFactory into LogOptions for the sample app.
Sandbox/LoggerFactory.cs Provides a Serilog-backed Microsoft.Extensions.Logging.ILoggerFactory for the sandbox.
UtilitiesTests/UtilitiesTests.csproj Adds AwesomeAssertions + logging abstractions, removes Serilog package reference.
UtilitiesTests/GlobalUsings.cs Adds global using for AwesomeAssertions.
UtilitiesTests/LogOptionsTests.cs Adds tests validating LogOptions factory behavior and logger creation.
UtilitiesTests/StringHistoryTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/StringCompressionTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/StringCompressionAsyncTests.cs Converts assertions to AwesomeAssertions (including async exception assertions).
UtilitiesTests/ExtensionsTests.cs Updates logger helper tests to use Microsoft.Extensions.Logging and converts assertions.
UtilitiesTests/FormatTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/FileTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/FileExAsyncTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/DownloadTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/DownloadAsyncTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/ConsoleTests.cs Converts assertions to AwesomeAssertions.
UtilitiesTests/CommandLineTests.cs Converts assertions to AwesomeAssertions.
README.md Updates release note summary for v3.7 to describe the logging model change.
HISTORY.md Adds detailed v3.7 release notes for the logging refactor and breaking changes.

Comment thread UtilitiesTests/LogOptionsTests.cs
Comment thread Utilities/LogOptions.cs
Comment thread Utilities/LogOptions.cs
Comment thread Utilities/LogOptions.cs
Comment thread HISTORY.md Outdated
@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 35.22727% with 57 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.77%. Comparing base (17b9cbd) to head (94418b9).

Files with missing lines Patch % Lines
Utilities/FileEx.cs 3.70% 52 Missing ⚠️
Utilities/Download.cs 60.00% 4 Missing ⚠️
Utilities/LogOptions.cs 95.45% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #381      +/-   ##
===========================================
+ Coverage    36.55%   42.77%   +6.21%     
===========================================
  Files           10       10              
  Lines         1045     1010      -35     
  Branches        93      100       +7     
===========================================
+ Hits           382      432      +50     
+ Misses         639      532     -107     
- Partials        24       46      +22     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI review requested due to automatic review settings July 9, 2026 23:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 26 out of 26 changed files in this pull request and generated 7 comments.

Comment thread UtilitiesTests/FormatTests.cs Outdated
Comment thread UtilitiesTests/FormatTests.cs Outdated
Comment thread UtilitiesTests/FileTests.cs Outdated
Comment thread UtilitiesTests/FileTests.cs Outdated
Comment thread Utilities/LogOptions.cs
Comment thread Utilities/LogOptions.cs
Comment thread Utilities/LogOptions.cs Outdated
- Read LoggerFactory once in CreateLogger so a concurrent swap cannot
  split the reference check and logger creation across two instances.
- Correct the AwesomeAssertions subject/expected order in FormatTests
  and FileTests so failure messages report actual vs expected correctly.
- Add a CreateLogger<T> test covering the generic category overload.
- Add codecov.yml: ignore the Sandbox example app and make the coverage
  statuses report-only, matching the report-only upload configuration.
Copilot AI review requested due to automatic review settings July 9, 2026 23:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 27 out of 28 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 9, 2026 23:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 27 out of 28 changed files in this pull request and generated no new comments.

@ptr727
ptr727 merged commit d941159 into develop Jul 9, 2026
12 checks passed
ptr727 added a commit that referenced this pull request Jul 10, 2026
Promotes the 3.7 release from develop to main.

**Contents**
- #381 - Adopt an injectable `Microsoft.Extensions.Logging` logging
model: `LogOptions.SetFactory(ILoggerFactory)` replaces the removed
Serilog-typed `LogOptions.Logger`; source-generated `[LoggerMessage]`
logging in `FileEx`/`Download`; `Extensions` renamed to
`CompressExtensions`; Serilog-wired `Sandbox`; test suite migrated to
AwesomeAssertions.
- #382 - Gitignore test coverage output and drop an
accidentally-committed report.

**Release impact:** merging publishes the stable **3.7** package to
NuGet (breaking API changes: `LogOptions.Logger` removed, `Extensions`
renamed). No issues to close.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
ptr727 added a commit to ptr727/LanguageTags that referenced this pull request Jul 10, 2026
Fixes #252.

## Problem
`LogOptions.CreateLogger(string)` read the `LoggerFactory` property
twice — once for the `ReferenceEquals` guard and once for the actual
`CreateLogger` call. Since the backing field is swapped with
`Interlocked.Exchange`, a concurrent `SetFactory` could make the guard
and the create observe different factory instances, defeating the
thread-safe intent of the `Volatile`/`Interlocked` design.

## Fix
Read the factory once into a local and use it for both the check and the
create. Also splits the `<remarks>` em-dash into two ASCII sentences,
matching the sibling `ptr727.Utilities` fix (ptr727/Utilities#381).

## Verification
`dotnet test` — build clean, 296/296 pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to 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.

2 participants