RedLock.net icon indicating copy to clipboard operation
RedLock.net copied to clipboard

Improve Logging to use static expressions

Open Dragonsangel opened this issue 3 years ago • 1 comments

The logs that are currently written by RedLock.net, are formatted at the time of writing the log entry. Example from the current code base: https://github.com/samcook/RedLock.net/blob/dcfe373b3e288caac4f45b78c88b5943f8b47e42/RedLockNet.SERedis/RedLock.cs#L73 This would result in a log entry to be written as Expiry time 5ms too low, setting to 10ms where the message_template in the background would be Expiry time 5ms too low, setting to 10ms. This makes it difficult to search or filter on log entries in log aggregators, since the formatted string could potentially be any value.

From the Roslyn Analyser, there is warning "CA2254: Template should be a static expression", explicitly for these type of log entries, to allow for easier searching and filtering in log aggregators. If the log writing example was to be changed to this

logger.LogWarning("Expiry time {ExpiryTotalMilliseconds}ms too low, setting to {MinimumExpiryTotalMilliseconds}ms", expiryTime.TotalMilliseconds, MinimumExpiryTime.TotalMilliseconds);

the resulting log entry would still be written as Expiry time 5ms too low, setting to 10ms but the message_template in the background would be Expiry time {ExpiryTotalMilliseconds}ms too low, setting to {MinimumExpiryTotalMilliseconds}ms which can be used to filter on all occurrences of this log entry, regardless of the values of expiryTime.TotalMilliseconds or MinimumExpiryTime.TotalMilliseconds

Here is the usage of Log message template explained further, with examples of formatting. One additional suggestion I would like to make, is that the placeholder names be consistently case formatted, so either always upper CamelCase or lower camelCase. If you can tell me which formatting you would prefer, I could make a PR with all the logging calls converted.

Dragonsangel avatar Jan 31 '23 16:01 Dragonsangel

I just saw that all the mentioned changes are already in PR #98

Dragonsangel avatar Oct 16 '23 12:10 Dragonsangel