Skip to content

Log every use case, unexpected errors and requests once each - #40

Merged
stphnwlsh merged 2 commits into
mainfrom
feature/logging
Nov 3, 2026
Merged

stphnwlsh merged 2 commits into
mainfrom
feature/logging

Conversation

@stphnwlsh

@stphnwlsh stphnwlsh commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Until now nothing we wrote logged anything, and the one log line per request was wrong for errors. This gives each request a clear trail, the same as CleanMinimalApi #66.

  • LoggingBehaviour wraps every use case and logs how it ended, at Information, with the request name and its time in milliseconds: handled, failed validation (with the property names, never the values) or found nothing. It's registered before ValidationBehaviour, so it sees validation failures too. The messages are [LoggerMessage] source-generated, and the time comes from TimeProvider.
  • Unexpected exceptions are logged. ApplicationExceptionHandler now logs anything it turns into a 500 as Error, with the exception. Handling an exception stops ASP.NET Core logging it, so before this a crash left only a status code behind.
  • The request log runs before the exception handler. It used to sit after it, so it saw the exception on its way out and logged every 400 and 404 as a 500, stack trace and all. Now each request is logged once with the status the client got.
  • Information is the default level, and appsettings.local.json lowers it to Debug.
  • TimeProvider.System is registered with TryAddSingleton in both AddApplication and AddInfrastructure, so each layer works on its own without registering the clock twice.
  • FakeLogger (Microsoft.Extensions.Diagnostics.Testing, MIT) checks the messages in the behaviour and handler tests.
  • The serilog, mediator, time-provider, errors-and-exceptions, unit-tests and api-conventions skills describe all of this.

@github-actions github-actions Bot added the enhancement New feature or request label Nov 3, 2026
@stphnwlsh stphnwlsh self-assigned this Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Build Pipeline

✅ Ready to merge. Every stage passed.

Stage Result Details
Build 🟢 Passed Version 2026.11.03.141-pr-40 · Template package 191 KB
Test 🟢 Passed 222 passed · 0 failed · 0 skipped · Template installs, builds and passes its tests
Coverage 🟢 Passed 100% (602 of 602 lines) · Needs 95%
Static Analysis 🟢 Passed No open CodeQL alerts
Dependency Review 🟢 Passed 0 vulnerable packages · 0 licence problems
Container Scan 🟢 Passed Image starts and answers · No open Trivy alerts

Commit 18ce52d · Full run

@stphnwlsh
stphnwlsh merged commit 9da70b5 into main Nov 3, 2026
13 checks passed
@stphnwlsh
stphnwlsh deleted the feature/logging branch November 3, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

1 participant