Skip to content

DI-injected logger, attempt 2#775

Merged
Saancreed merged 1 commit into
v9.0-previewfrom
feature/di-injected-logger-v2
May 20, 2025
Merged

DI-injected logger, attempt 2#775
Saancreed merged 1 commit into
v9.0-previewfrom
feature/di-injected-logger-v2

Conversation

@Saancreed
Copy link
Copy Markdown
Member

@Saancreed Saancreed commented May 14, 2025

Supersedes #757.

I'll probably squash all that in the end, but for now I kept my own commit separate for easier reviewing.

The idea is to have one slim package that references Serilog and has only interfaces, adapters and null logger, with the rest moved to a new package. Naming suggestions are welcome; LeanCode.Logging.Loggers is kind of horrible.

TODO: reorder constructor parameters to match the logger-is-last convention?

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 14, 2025

test: Run #2170

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Pending ⏳ Other ❓ Flaky 🍂 Duration ⏱️
751 750 0 1 0 0 0 209ms

🎉 All tests passed!

Github Test Reporter

🔄 This comment has been updated

Copy link
Copy Markdown
Member

@jakubfijalkowski jakubfijalkowski left a comment

Choose a reason for hiding this comment

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

About the Leancode.Logging.Loggers - LeanCode.Logging.AspNetCore, as the main use case is to integrate the LeanCode.Logging package with ASP.NET (analogous to LeanCode.CQRS.AspNetCore).

Comment thread src/Infrastructure/LeanCode.Logging/LeanCodeLoggingBuilderExtensions.cs Outdated

public static ILoggingBuilder TryAddContextualLeanCodeLogger(this ILoggingBuilder builder)
{
builder.Services.TryAddSingleton(typeof(LeanCode.Logging.ILogger<>), typeof(ContextualLogger<>));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Transient, not singleton - otherwise the loggers will stay instantiated even if the class using them will be long gone (think rarely invoked handlers).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wouldn't that cause DI validation to complain if we try to resolve this logger in a service registered as singleton?

And even if not, wouldn't Scoped be better than Transient? I don't see a reason to instanciate the logger for the same context multiple times per request.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good point, dunno how to tackle the singleton problem. :( Nevertheless, the main usecase is "quickly instantiate logger for a very short time", so would go with it either way (which also would work the same way as previously when it comes to GC/instantiation).

Ad. Scoped/Transient - yep, you're right. I forgot the nomenclature.

@Saancreed Saancreed marked this pull request as ready for review May 16, 2025 12:01
Comment on lines +13 to +16
return services.AddSingleton<IViewRenderer>(sp => new RazorViewRenderer(
config,
sp.GetRequiredService<ILogger<RazorViewRenderer>>()
));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

seems out of scope

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Indeed, but it was a small inconsistency I wanted to fix along the way.

Assert.Equal("Three", evt.MessageTemplate.Text);
}

internal class SingleLogEventCapturerSink : ILogEventSink
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👀

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, might as well.

Co-authored-by: Krzysztof Bogacki <krzysztof.bogacki@leancode.pl>
@Saancreed Saancreed force-pushed the feature/di-injected-logger-v2 branch from 4fe8da4 to 8cd227a Compare May 20, 2025 11:14
@Saancreed Saancreed merged commit a52234e into v9.0-preview May 20, 2025
7 checks passed
@Saancreed Saancreed deleted the feature/di-injected-logger-v2 branch May 20, 2025 11:26
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (f85ab80) to head (8cd227a).
Report is 6 commits behind head on v9.0-preview.

Additional details and impacted files
@@         Coverage Diff         @@
##   v9.0-preview   #775   +/-   ##
===================================
===================================

☔ View full report in Codecov by Sentry.
📢 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.

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.

3 participants