Refactor HTTP logging handler for efficiency#1649
Open
paulomorgado wants to merge 1 commit into
Open
Conversation
Refactored SendAsync to skip logging when not enabled and moved logging logic to a dedicated method. Switched requestId generation to use a cryptographically secure hex string. Improved log level checks to reduce unnecessary work. Enhanced body logging to respect debug level and _logBody flag. Simplified JSON detection and body truncation logic, introducing a constant and StringBuilder for efficiency. Made minor code structure and formatting improvements.
30bf57e to
56b0114
Compare
sipsorcery
reviewed
May 30, 2026
| { | ||
| // Log request with better formatting | ||
| var requestId = Guid.NewGuid().ToString("N").Substring(0, 8); // Short ID for correlation | ||
| if (!_logger.IsEnabled(LogLevel.Information)) |
Member
There was a problem hiding this comment.
Shouldn't this be LogLevel.Debug to match what's in SendAsyncWithLogging?
Contributor
Author
There was a problem hiding this comment.
Let me think.
If LogLevel.Debug is enabled, then LogLevel.Information is enabled. Right?
So, SendAsyncWithLogging is only invoked it LogLevel.Information is enabled.
If LogLevel.Debug is enabled, the debug logging is also executed.
Right?
Member
There was a problem hiding this comment.
Yes, but if it is the Information level enabled, and not Debug, then it will go into SendAsyncWithLogging and then fail. It may as well short circuit before calling SendAsyncWithLogging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactored SendAsync to skip logging when not enabled and moved logging logic to a dedicated method. Switched requestId generation to use a cryptographically secure hex string. Improved log level checks to reduce unnecessary work. Enhanced body logging to respect debug level and _logBody flag. Simplified JSON detection and body truncation logic, introducing a constant and StringBuilder for efficiency. Made minor code structure and formatting improvements.