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

Optimize logger #3370

Open
wants to merge 8 commits into
base: v4-development
Choose a base branch
from

Conversation

danielmarbach
Copy link
Contributor

Description

This PR removes the global lock on the logger and introduces currently internal properties to check on the hot path whether the logger is enabled to avoid resource intensive operations for log levels that are not enabled.

Motivation and Context

When requests are canonicalized (is that even a word :D) the resource path runs for the canonicalization process which then acquires a logger. This acquires a global lock to then access a dictionary that quite likely already contains the logger. Furthermore it always allocates strings for debug logging that might not be required when that log level is not even requested.

Testing

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed

License

  • I confirm that this pull request can be released under the Apache 2 license

@danielmarbach danielmarbach changed the base branch from main to v4-development July 2, 2024 20:05
@danielmarbach
Copy link
Contributor Author

Some of these changes might even benefit from being cherry picked to the current released versions

Comment on lines +123 to +129
foreach (var item in oldLoggerCache ?? Enumerable.Empty<KeyValuePair<Type, Lazy<Logger>>>())
{
var lazyLogger = item.Value;
if (lazyLogger.IsValueCreated)
{
lazyLogger.Value.Unregister();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have realized the config change handler is never unregistered and effectively the old logger instances can still be rooted.

@@ -80,39 +77,76 @@ private void ConfigureLoggers()
il.IsEnabled = (logging & LoggingOptions.Console) == LoggingOptions.Console;
if (il is InternalSystemDiagnosticsLogger)
il.IsEnabled = (logging & LoggingOptions.SystemDiagnostics) == LoggingOptions.SystemDiagnostics;

if (!IsEnabled)
Copy link
Member

Choose a reason for hiding this comment

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

For all of these properties the last InternalLogger will overwrite the value on the Logger. If I understand the purpose the properties on the Logger should be true if any of the InternalLogger are true.

Copy link
Contributor Author

@danielmarbach danielmarbach Jul 4, 2024

Choose a reason for hiding this comment

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

I do suck at boolean logic and would probably fail any job interview 👯‍♂️

Isn't that exactly what it is doing? If the property is false it will check the property of the internal logger and set it to the corresponding value of the internal logger. If the internal logger is true it sets the property to true and subsequent checks don't need to look at the internal logger value anymore. If the property gets set to false the next iteration needs to check the value of the next internal logger. So if all are false it remains false. If one is true it will turn to true and subsequent checks omit toggling it. I wanted to avoid Any()

https://dotnetfiddle.net/Se39FX

@danielmarbach danielmarbach force-pushed the logger-check-enabled-on-hotpath branch from e670efd to 145021c Compare July 4, 2024 08:09
@danielmarbach
Copy link
Contributor Author

@dscpinheiro I think this is ready for review. See also my answer to @normj

@danielmarbach
Copy link
Contributor Author

Ping @normj @dscpinheiro

@danielmarbach
Copy link
Contributor Author

Is there anything else required here to be able to move this forward?

@danielmarbach
Copy link
Contributor Author

Ping @normj @dscpinheiro

@danielmarbach danielmarbach force-pushed the logger-check-enabled-on-hotpath branch from 145021c to ffca58c Compare November 9, 2024 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants