Skip to content

Commit

Permalink
#4: Change the high-performance logging methods to extension methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsagara committed Dec 22, 2023
1 parent bafd1ac commit 9195961
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
push:
branches:
- 'main' # Run the workflow when pushing to the main branch
- 'release/**' # Run the workflow when pushing to a release branch
pull_request:
branches:
- '*' # Run the workflow for all pull requests
Expand Down Expand Up @@ -90,7 +91,7 @@ jobs:
# Publish only when creating a GitHub Release
# https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository
# You can update this logic if you want to manage releases differently
# if: github.event_name == 'release'
if: github.event_name == 'release'
runs-on: ubuntu-latest
needs: [ pack_nuget, run_test ]
steps:
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>12.0</LangVersion>

<!-- NuGet -->
<Version>2.0.9</Version>
<Version>2.0.10</Version>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Authors>Jon Sagara</Authors>
Expand Down
2 changes: 1 addition & 1 deletion src/Sagara.Core.Caching/RedisAdminCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ await server
catch (Exception ex)
{
// Don't let the cache server bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "FLUSHALL", key: "(all keys)");
_logger.UnhandledException(ex, command: "FLUSHALL", key: "(all keys)");
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/Sagara.Core.Caching/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ protected ConnectionMultiplexer InitializeConnectionMultiplexer(ConfigurationOpt
// Log connection and error events.
multiplexer.ConnectionFailed += (sender, e) =>
{
RedisCacheLogger.OnConnectionFailed(_logger, e.Exception, logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
_logger.OnConnectionFailed(e.Exception, logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
};

multiplexer.ConnectionRestored += (sender, e) =>
{
RedisCacheLogger.OnConnectionRestored(_logger, e.Exception, logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
_logger.OnConnectionRestored(e.Exception, logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
};

multiplexer.ErrorMessage += (sender, e) =>
{
RedisCacheLogger.OnErrorMessage(_logger, logPrefix, e.EndPoint, e.Message);
_logger.OnErrorMessage(logPrefix, e.EndPoint, e.Message);
};

multiplexer.ServerMaintenanceEvent += (sender, e) =>
{
RedisCacheLogger.OnServerMaintenanceEvent(_logger, logPrefix, e.ReceivedTimeUtc, e.StartTimeUtc, e.RawMessage);
_logger.OnServerMaintenanceEvent(logPrefix, e.ReceivedTimeUtc, e.StartTimeUtc, e.RawMessage);
};

return multiplexer;
Expand Down Expand Up @@ -107,7 +107,7 @@ protected ConnectionMultiplexer InitializeConnectionMultiplexer(ConfigurationOpt
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "GET", key: key);
_logger.UnhandledException(ex, command: "GET", key: key);
}

return default;
Expand Down Expand Up @@ -155,7 +155,7 @@ protected ConnectionMultiplexer InitializeConnectionMultiplexer(ConfigurationOpt
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "GET/EXPIRE", key: key);
_logger.UnhandledException(ex, command: "GET/EXPIRE", key: key);
}

return default;
Expand Down Expand Up @@ -189,7 +189,7 @@ protected ConnectionMultiplexer InitializeConnectionMultiplexer(ConfigurationOpt
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "GET/EXPIRE", key: key);
_logger.UnhandledException(ex, command: "GET/EXPIRE", key: key);
}

return default;
Expand Down Expand Up @@ -219,7 +219,7 @@ public async Task<bool> SetAsync(string key, object value, TimeSpan? expiry = nu
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "SET", key: key);
_logger.UnhandledException(ex, command: "SET", key: key);
}

return false;
Expand Down Expand Up @@ -247,7 +247,7 @@ public bool Set(string key, object value, TimeSpan? expiry = null)
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "SET", key: key);
_logger.UnhandledException(ex, command: "SET", key: key);
}

return false;
Expand All @@ -274,7 +274,7 @@ public bool Set(string key, object value, TimeSpan? expiry = null)
// catch (Exception ex)
// {
// // Don't let cache server unavailability bring down the application.
// RedisCacheLogger.UnhandledException(_logger, ex, command: "INCR", key: key);
// _logger.UnhandledException(ex, command: "INCR", key: key);
// }

// return 0L;
Expand All @@ -300,7 +300,7 @@ public async Task<bool> RemoveAsync(string key)
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "DEL", key: key);
_logger.UnhandledException(ex, command: "DEL", key: key);
}

return false;
Expand Down Expand Up @@ -331,7 +331,7 @@ public async Task<long> RemoveAsync(IReadOnlyCollection<string> keys)
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "DEL", key: string.Join(" ", keys));
_logger.UnhandledException(ex, command: "DEL", key: string.Join(" ", keys));
}
}

Expand Down Expand Up @@ -380,7 +380,7 @@ public async Task<long> IncrementAndExpireOnCreateAsync(string key, TimeSpan exp
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledException(_logger, ex, command: "INCR and EXPIRE On Create", key: key);
_logger.UnhandledException(ex, command: "INCR and EXPIRE On Create", key: key);
}

return 0L;
Expand Down Expand Up @@ -414,7 +414,7 @@ await GetSubscriber()
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledSubscribeException(_logger, ex, channel);
_logger.UnhandledSubscribeException(ex, channel);
}
}

Expand All @@ -432,7 +432,7 @@ public async Task<long> PublishAsync(RedisChannel channel, RedisValue message)
catch (Exception ex)
{
// Don't let cache server unavailability bring down the application.
RedisCacheLogger.UnhandledPublishException(_logger, ex, channel, message);
_logger.UnhandledPublishException(ex, channel, message);
}

return 0L;
Expand Down
14 changes: 7 additions & 7 deletions src/Sagara.Core.Caching/RedisCacheLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ namespace Sagara.Core.Caching;
internal static partial class RedisCacheLogger
{
[LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "{LogPrefix}ConnectionFailed: Connection type '{ConnectionType}' on EndPoint '{EndPoint}' reported ConnectionFailureType '{FailureType}'")]
public static partial void OnConnectionFailed(ILogger logger, Exception? ex, string logPrefix, ConnectionType connectionType, EndPoint? endPoint, ConnectionFailureType failureType);
public static partial void OnConnectionFailed(this ILogger logger, Exception? ex, string logPrefix, ConnectionType connectionType, EndPoint? endPoint, ConnectionFailureType failureType);

[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "{LogPrefix}ConnectionRestored: Connection type '{ConnectionType}' on EndPoint '{EndPoint}' reported ConnectionFailureType '{FailureType}'")]
public static partial void OnConnectionRestored(ILogger logger, Exception? ex, string logPrefix, ConnectionType connectionType, EndPoint? endPoint, ConnectionFailureType failureType);
public static partial void OnConnectionRestored(this ILogger logger, Exception? ex, string logPrefix, ConnectionType connectionType, EndPoint? endPoint, ConnectionFailureType failureType);

[LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "{LogPrefix}ErrorMessage: Server '{EndPoint}' reported this error message: {Message}")]
public static partial void OnErrorMessage(ILogger logger, string logPrefix, EndPoint? endPoint, string message);
public static partial void OnErrorMessage(this ILogger logger, string logPrefix, EndPoint? endPoint, string message);

[LoggerMessage(EventId = 0, Level = LogLevel.Warning, Message = "{LogPrefix}ServerMaintenanceEvent: Server maintenance event received at {ReceivedTimeUtc}. Expected start time is {StartTimeUtc}. Raw message: {RawMessage}")]
public static partial void OnServerMaintenanceEvent(ILogger logger, string logPrefix, DateTime receivedTimeUtc, DateTime? startTimeUtc, string? rawMessage);
public static partial void OnServerMaintenanceEvent(this ILogger logger, string logPrefix, DateTime receivedTimeUtc, DateTime? startTimeUtc, string? rawMessage);

[LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "Unhandled exception trying to async {Command} {Key}")]
public static partial void UnhandledException(ILogger logger, Exception ex, string command, string key);
public static partial void UnhandledException(this ILogger logger, Exception ex, string command, string key);

[LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "Unhandled exception trying to SUBSCRIBE {Channel}")]
public static partial void UnhandledSubscribeException(ILogger logger, Exception ex, RedisChannel channel);
public static partial void UnhandledSubscribeException(this ILogger logger, Exception ex, RedisChannel channel);

[LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "Unhandled exception trying to async PUBLISH {Channel} {Message}")]
public static partial void UnhandledPublishException(ILogger logger, Exception ex, RedisChannel channel, RedisValue message);
public static partial void UnhandledPublishException(this ILogger logger, Exception ex, RedisChannel channel, RedisValue message);
}

0 comments on commit 9195961

Please sign in to comment.