Skip to content

Add support for provider registration in logging #20215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Add support for provider registration in logging #20215

wants to merge 1 commit into from

Conversation

captainsafia
Copy link
Member

@captainsafia captainsafia commented Mar 26, 2020

Changes in this PR

  • Adds support for provider registration in logger factory
  • Updates PrependMessageLoggerFactory in test app to use provider model
  • Updates WebAssemblyConsoleLogger to use provider model

Most of the logic in WebAssemblyLogger, WebAssemblyLoggerFactory, and WebAssemblyLoggerInformation is lifted from the implementations in Microsoft.Extensions.Logging.

Addresses #19737

@captainsafia captainsafia added the area-blazor Includes: Blazor, Razor Components label Mar 26, 2020
@captainsafia captainsafia added this to the blazor-wasm-3.2-preview4 milestone Mar 26, 2020
@@ -0,0 +1,59 @@
// Copyright (c) .NET Foundation. All rights reserved.
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll be working on adding more tests here but this is ready for review in the meanwhile.

@javiercn
Copy link
Member

Looks like a great starting point!

It would be great if you change the standalone app to use the same pattern ASP.NET Core apps use to configure logging through config. (If it's picked automatically).

The other thing I would point out, is that the way of configuring logging seems a bit different than regular ASP.NET Core apps. Should we make the pattern similar?

@SteveSandersonMS
Copy link
Member

@captainsafia Is it OK if I review this tomorrow? (Currently I'm really focused on getting some things ready for the ASP.NET community standup later today)

@captainsafia
Copy link
Member Author

captainsafia commented Mar 30, 2020

Looks like a great starting point!

It would be great if you change the standalone app to use the same pattern ASP.NET Core apps use to configure logging through config. (If it's picked automatically).

The other thing I would point out, is that the way of configuring logging seems a bit different than regular ASP.NET Core apps. Should we make the pattern similar?

I was planning on doing the configuration bits in a separate PR, but I'll tack it all into this one since it's the last week for preview4 work.

@captainsafia Is it OK if I review this tomorrow? (Currently I'm really focused on getting some things ready for the ASP.NET community standup later today)

Sure. I will reping team once this is ready for review with the configuration changes.

/// <inheritdoc />
public ILogger CreateLogger(string name)
{
return _loggers.GetOrAdd(name, loggerName => new WebAssemblyConsoleLogger<object>(name, _jsRuntime));
Copy link
Member

@SteveSandersonMS SteveSandersonMS Mar 31, 2020

Choose a reason for hiding this comment

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

Maybe this implementation comes from somewhere else, but I'm surprised it's not an error to call this with a name that's already registered, since the underlying dictionary can't store more than one with the same name.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is based on the implementation of the ConsoleLoggerProvider in Microsoft.Extensions.Logging.

since the underlying dictionary can't store more than one with the same name.

Hm. GetOrAdd is used here so that constraint should be satisfied even if the user calls CreateLogger("someName") twice.

{
if (!_disposed)
{
_loggers = null;
Copy link
Member

Choose a reason for hiding this comment

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

Is there any chance we need to Dispose the ILogger instances before nulling this out?

exceptions.Add(ex);
}

return false;
Copy link
Member

Choose a reason for hiding this comment

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

This logic to aggregate exceptions looks super robust, though TBH I'm not certain what the use case for it is. If we didn't catch any of this, and just let the first exception be unhandled (since it's never a legit scenario to have loggers throw), would that be sufficient? Are there cases where it's necessary to collect more information than that?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is heavily inspired by the Logger implementation in Microsoft.Extensions.Logging.

The intent behind this logic was to collect all the exceptions that arise from each logger that is registered and throw an aggregate exception for them. It is possible to bail and throw at the first exception thrown from a logger but I figured I would keep this logic because:

  • It's more helpful to the developer to get all the exceptions from buggy loggers instead of getting them one-by-one.
  • Wanted to follow existing experiences that devs would be used to. This didn't seem, outrageous enough to nuke.

@pranavkm pranavkm requested review from 333fred and BrennanConroy and removed request for 333fred April 1, 2020 16:12
@captainsafia
Copy link
Member Author

Closing this PR in favor of an approach that relies on the Logger factory within Microsoft.Extensions.Logging.

The WebAssemblyLoggerFactory was originally included in the code base because there was a concern that bringing in Microsoft.Extensions.Logging as a dependency would increase the total size of DLLs that would have to be pulled down.

I ran some quick validations on the BasicTestApp to see the size difference between the compressed DLLs that including the reference would bring. It's negligible enough that we can proceed with an approach that doesn't use a custom logger factory.

With package Without package
Linker on 1361 kb 1351 kb
Linker off 5778 kb 5761 kb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants