Skip to content

feat: ILoggingBuilder.AddSerilog() extension method#3013

Draft
jonpryor wants to merge 1 commit intomainfrom
dev/jonpryor/jonp-AddSerilog-extension-method
Draft

feat: ILoggingBuilder.AddSerilog() extension method#3013
jonpryor wants to merge 1 commit intomainfrom
dev/jonpryor/jonp-AddSerilog-extension-method

Conversation

@jonpryor
Copy link
Copy Markdown
Contributor

Context: #3008
Context: #3011
Context: unoplatform/uno.templates#1921

Issue #3008 details that there are two ILogger "environments" in a Uno app:

  • The App.InitializeLogging() environment ("NoDI"), and
  • The environment configured by Dependency Injection methods such as .UseLogging() and .UseSerilog() ("DI").

The problem is that "DI" logging cannot capture log messages produced before the DI environment is built, frequently via builder.Build() or builder.NavigateAsync<TShell>(). A lot of code can execute before those methods complete.

A further problem is that no default template actually configures both environments: if you use dotnet new unoapp -di False you get the NoDI App.InitializeLogging() environment, which will not log messages from the Dependency Injection enviornment. If you use dotnet new unoapp -preset "recommended" you get only the DI-based logging, thus missing all messages from the NoDI environment.

Thus, the idea for "ILogger configuration unification": What If instead of having two separate places for logging, we just had one: the NoDI App.InitializeLogging() location.

This can be made to work via #3011, which updates
HostBuilder.Build() so that if
LogExtensionPoint.AmbientLoggerFactory is set, it replaces the ILoggerFactory that HostBuilder.Build() normally configures. This allows App.InitializeLogging() to also be used in the DI environment.

So far, so useful.

But what about Serilog? Serilog is a widely recommended logger library, and the current .UseSerilog() extension method requires a full Dependency Injection environment: it cannot be invoked within App.InitializeLogging().

Try to square this circle by adding a new
ILoggingBuilder.AddSerilog() extension method. This would allow Serilog to be configured within App.InitializeLogging(), helping to support a unified logging setup experience.

GitHub Issue (If applicable): closes #

PR Type

What kind of change does this PR introduce?

What is the current behavior?

What is the new behavior?

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Docs have been added/updated which fit documentation template. (for bug fixes / features)
  • Unit Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Wasm UI Tests are not showing unexpected any differences. Validate PR Screenshots Compare Test Run results.
  • Contains NO breaking changes
  • Updated the Release Notes
  • Associated with an issue (GitHub or internal)

Other information

Internal Issue (If applicable):

Context: #3008
Context: #3011
Context: unoplatform/uno.templates#1921

Issue #3008 details that there are two `ILogger` "environments" in
a Uno app:

  * The `App.InitializeLogging()` environment ("NoDI"), and
  * The environment configured by Dependency Injection methods such
    as `.UseLogging()` and `.UseSerilog()` ("DI").

The problem is that "DI" logging *cannot* capture log messages
produced before the DI environment is built, frequently via
`builder.Build()` or `builder.NavigateAsync<TShell>()`.
*A lot* of code can execute before those methods complete.

A further problem is that no default template actually configures
both environments: if you use `dotnet new unoapp -di False` you
get the NoDI `App.InitializeLogging()` environment, which
*will not* log messages from the Dependency Injection enviornment.
If you use `dotnet new unoapp -preset "recommended"` you get
*only* the DI-based logging, thus missing all messages from the
NoDI environment.

Thus, the idea for "`ILogger` configuration unification": What If
instead of having two separate places for logging, we just had one:
the NoDI `App.InitializeLogging()` location.

This can be made to work via #3011, which updates
`HostBuilder.Build()` so that *if*
`LogExtensionPoint.AmbientLoggerFactory` is set, it *replaces* the
`ILoggerFactory` that `HostBuilder.Build()` normally configures.
This allows `App.InitializeLogging()` to also be used in the DI
environment.

So far, so useful.

But what about [Serilog][0]?  Serilog is a widely recommended logger
library, and the current `.UseSerilog()` extension method *requires*
a full Dependency Injection environment: it cannot be invoked within
`App.InitializeLogging()`.

Try to square this circle by adding a new
`ILoggingBuilder.AddSerilog()` extension method.  This would allow
Serilog to be configured within `App.InitializeLogging()`, helping
to support a unified logging setup experience.

[0]: https://platform.uno/docs/articles/external/uno.extensions/doc/Overview/Logging/LoggingOverview.html#serilog
@jonpryor
Copy link
Copy Markdown
Contributor Author

TODO/question to consider: what would be the replacement for loggerConfiguration.ReadFrom.Configuration(context.Configuration)?

@github-actions
Copy link
Copy Markdown

Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-moss-0c5b8040f-3013.eastus2.azurestaticapps.net

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.

1 participant