Skip to content

Commit

Permalink
Use high performance logging in RedisCache.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsagara committed Sep 24, 2023
1 parent c0b0808 commit 3d75927
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
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.0-rc2</Version>
<Version>2.0.0-rc3</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.
_logger.LogError(ex, "Unhandled exception while trying to delete all the keys of all databases on the server.");
RedisCacheLogger.UnhandledException(_logger, 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) =>
{
_logger.LogError(e.Exception, "{LogPrefix}ConnectionFailed: Connection type '{ConnectionType}' on EndPoint '{EndPoint}' reported ConnectionFailureType '{FailureType}'", logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
RedisCacheLogger.OnConnectionFailed(_logger, e.Exception, logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
};

multiplexer.ConnectionRestored += (sender, e) =>
{
_logger.LogInformation(e.Exception, "{LogPrefix}ConnectionRestored: Connection type '{ConnectionType}' on EndPoint '{EndPoint}' reported ConnectionFailureType '{FailureType}'", logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
RedisCacheLogger.OnConnectionRestored(_logger, e.Exception, logPrefix, e.ConnectionType, e.EndPoint, e.FailureType);
};

multiplexer.ErrorMessage += (sender, e) =>
{
_logger.LogError("{LogPrefix}ErrorMessage: Server '{EndPoint}' reported this error message: {Message}", logPrefix, e.EndPoint, e.Message);
RedisCacheLogger.OnErrorMessage(_logger, logPrefix, e.EndPoint, e.Message);
};

multiplexer.ServerMaintenanceEvent += (sender, e) =>
{
_logger.LogWarning("{LogPrefix}ServerMaintenanceEvent: Server maintenance event received at {ReceivedTimeUtc}. Expected start time is {StartTimeUtc}. Raw message: {RawMessage}", logPrefix, e.ReceivedTimeUtc, e.StartTimeUtc, e.RawMessage);
RedisCacheLogger.OnServerMaintenanceEvent(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to async GET {Key}", key);
RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to async GET/EXPIRE {Key}", key);
RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to GET/EXPIRE {Key}", key);
RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to async SET {Key}", key);
RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to SET {Key}", key);
RedisCacheLogger.UnhandledException(_logger, 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.
// _logger.LogError(ex, "Unhandled exception trying to INCR {Key}", key);
// RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to async DEL {Key}", key);
RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to async DEL {KeysJoined}", string.Join(" ", keys));
RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to INCR and EXPIRE On Create of the key {Key}", key);
RedisCacheLogger.UnhandledException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to SUBSCRIBE {Channel}", channel);
RedisCacheLogger.UnhandledSubscribeException(_logger, 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.
_logger.LogError(ex, "Unhandled exception trying to async PUBLISH {Channel} {Message}", channel, message);
RedisCacheLogger.UnhandledPublishException(_logger, ex, channel, message);
}

return 0L;
Expand Down
32 changes: 32 additions & 0 deletions src/Sagara.Core.Caching/RedisCacheLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Net;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;

namespace Sagara.Core.Caching;

/// <summary>
/// High-performance logging for ASP.NET Core. See: https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator
/// </summary>
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);

[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);

[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);

[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);

[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);

[LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "Unhandled exception trying to SUBSCRIBE {Channel}")]
public static partial void UnhandledSubscribeException(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);
}
3 changes: 0 additions & 3 deletions src/Sagara.Core.Caching/Sagara.Core.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<GenerateDocumentationFile>True</GenerateDocumentationFile>

<!-- Don't warn about the LoggerMessage pattern. -->
<NoWarn>$(NoWarn);CA1848</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 3d75927

Please sign in to comment.