Skip to content
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

Add a way to get the configured log level switches #347

Closed
wants to merge 5 commits into from

Conversation

0xced
Copy link
Member

@0xced 0xced commented Feb 6, 2023

I have created this pull request as a draft because a few things should be discussed before merging.

  1. I chose LoadedConfiguration for the class holding the loaded log level switches, as proposed by @tsimbalar in How to configure from JSON, but then get a reference to the switch? #206 (comment) I'm not convinced it's the best name and I'm happy to change it to something better.
  2. This LoadedConfiguration class could also expose the log filter switches, although not the internal LoggingFilterSwitchProxy class directly. Maybe through a public ILoggingFilterSwitch interface with a single Expression { get; set; } property?

I am looking forward to your feedback.

Fixes #206

/// The log level switches that were loaded from the <c>LevelSwitches</c> section of the configuration.
/// </summary>
/// <remarks>The keys of the dictionary are the name of the switches without the leading <c>$</c> character.</remarks>
public IReadOnlyDictionary<string, LoggingLevelSwitch> LogLevelSwitches => _logLevelSwitches.ToDictionary(e => e.Key.TrimStart('$'), e => e.Value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Property allocates Dictionary and N strings each call to it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if it is worth worrying about the initial dollar sign in the name. In fact, this is part of the switch name. What is the goal in removal of the dollar sign?

Copy link
Member Author

Choose a reason for hiding this comment

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

In fact, this is part of the switch name.

Well, this is debatable: the LevelSwitches section does not need to contain the $ prefix. As found in the README and the appsettings.json file of the Sample project.

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "LevelSwitches": { "controlSwitch": "Verbose" },

Property allocates Dictionary and N strings each call to it.

I wouldn't worry too much about that, one call is made per call to .ReadFrom.Configuration(configuration). So for most apps that would be exactly once at startup I guess.

Also, IDictionary does not implement IReadOnlyDictionary, sigh. But we could store Dictionary<string, LoggingLevelSwitch> instead of IDictionary<string, LoggingLevelSwitch> everywhere if the consensus is that this allocation must really be avoided.

Copy link
Contributor

Choose a reason for hiding this comment

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

the LevelSwitches section does not need to contain the $ prefix.

Then it makes no sense in cutting off the dollar sign if it is not a special symbol. Why to do this? Just return _logLevelSwitches as is.

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 have addressed this concern in 2ecbe0a.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 $ should be appended in tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Of course! Fixed in 3f459e1.

Co-authored-by: Ivan Maximov <[email protected]>
/// <summary>
/// The log level switches that were loaded from the <c>LevelSwitches</c> section of the configuration.
/// </summary>
/// <remarks>The keys of the dictionary are the name of the switches, including the leading <c>$</c> character.</remarks>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// <remarks>The keys of the dictionary are the name of the switches, including the leading <c>$</c> character.</remarks>
/// <remarks>The keys of the dictionary are the names of the switches, including the leading <c>$</c> character if any.</remarks>

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in c58ef69, but not the if any part because the switch always have a leading $ in the _declaredLevelSwitches dictionary:

public void AddLevelSwitch(string levelSwitchName, LoggingLevelSwitch levelSwitch)
{
    _declaredLevelSwitches[ToSwitchReference(levelSwitchName)] = levelSwitch;
}

string ToSwitchReference(string switchName)
{
    return switchName.StartsWith("$") ? switchName : $"${switchName}";
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. I didn't know.

@0xced
Copy link
Member Author

0xced commented Feb 9, 2023

Closing this pull request because it will be 200% better to work on top of #348 to provide access to the configured log level switches. 😎

@0xced 0xced closed this Feb 9, 2023
@0xced 0xced deleted the LoadedConfiguration branch March 9, 2023 19:24
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.

How to configure from JSON, but then get a reference to the switch?
2 participants