Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/SharedKernel/Extensions/SignalRExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using ResponseCrafter.ExceptionHandlers.SignalR;
using SharedKernel.Logging;
using StackExchange.Redis;

namespace SharedKernel.Extensions;

public static class SignalRExtensions
{

public static WebApplicationBuilder AddSignalR(this WebApplicationBuilder builder)
{
builder
.Services
.AddSignalR(o => o.AddFilter<SignalRExceptionFilter>())
.AddMessagePackProtocol();

builder.AddSignalRWithFiltersAndMessagePack();
return builder;
}

public static WebApplicationBuilder AddDistributedSignalR(this WebApplicationBuilder builder, string redisChannelName)

public static WebApplicationBuilder AddDistributedSignalR(this WebApplicationBuilder builder,
string redisChannelName)
{
builder
.Services
.AddSignalR(o => o.AddFilter<SignalRExceptionFilter>())
.AddMessagePackProtocol()
.AddStackExchangeRedis(builder.Configuration.GetRedisUrl(),
options =>
{
options.Configuration.ChannelPrefix = RedisChannel.Literal("FinHub:SignalR:");
});
builder.AddSignalRWithFiltersAndMessagePack()
.AddStackExchangeRedis(builder.Configuration.GetRedisUrl(),
options =>
{
options.Configuration.ChannelPrefix = RedisChannel.Literal("FinHub:SignalR:");
});


return builder;
}

private static ISignalRServerBuilder AddSignalRWithFiltersAndMessagePack(this WebApplicationBuilder builder)
{
return builder.Services
.AddSignalR(o =>
{
o.AddFilter<SignalRExceptionFilter>();
o.AddFilter<SignalRLoggingHubFilter>();
})
.AddMessagePackProtocol();
}
}
78 changes: 78 additions & 0 deletions src/SharedKernel/Logging/SignalRLoggingHubFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Diagnostics;
using System.Text.Json;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;

namespace SharedKernel.Logging;

internal sealed class SignalRLoggingHubFilter(ILogger<SignalRLoggingHubFilter> logger) : IHubFilter
{
public async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext,
Func<HubInvocationContext, ValueTask<object?>> next)
{
var start = Stopwatch.GetTimestamp();

// Basic context info
var hubName = invocationContext.Hub.GetType()
.Name;
var connectionId = invocationContext.Context.ConnectionId;
var userId = invocationContext.Context.UserIdentifier;
var methodName = invocationContext.HubMethodName;

// Redact arguments
var serializedArgs = JsonSerializer.Serialize(invocationContext.HubMethodArguments);
var redactedArgs = RedactionHelper.ParseAndRedactJson(serializedArgs);

object? result = null;
Exception? exception = null;

try
{
// Invoke the actual hub method
result = await next(invocationContext);
}
catch (Exception ex)
{
exception = ex;
}

var elapsedMs = Stopwatch.GetElapsedTime(start)
.TotalMilliseconds;

if (exception is not null)
{
logger.LogError(exception,
"[SignalR] Hub {HubName}, ConnId {ConnectionId}, UserId {UserId} - Method {MethodName} threw an exception after {ElapsedMs}ms. " +
"Inbound Args: {Args}",
hubName,
connectionId,
userId,
methodName,
elapsedMs,
redactedArgs);
throw exception;
}

// Redact return value, if any
var redactedResult = string.Empty;
if (result is not null)
{
var serializedResult = JsonSerializer.Serialize(result);
var redactedObj = RedactionHelper.ParseAndRedactJson(serializedResult);
redactedResult = JsonSerializer.Serialize(redactedObj);
}

logger.LogInformation(
"[SignalR] Hub {HubName}, ConnId {ConnectionId}, UserId {UserId} - Method {MethodName} completed in {ElapsedMs}ms. " +
"Inbound Args: {Args}, Outbound Result: {Result}",
hubName,
connectionId,
userId,
methodName,
elapsedMs,
redactedArgs,
redactedResult);

return result;
}
}
4 changes: 2 additions & 2 deletions src/SharedKernel/SharedKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Authors>Pandatech</Authors>
<Copyright>MIT</Copyright>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<PackageId>Pandatech.SharedKernel</PackageId>
<Title>Pandatech Shared Kernel Library</Title>
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
<PackageReleaseNotes>app.UseRequestResponseLogging() changed to app.UseRequestLogging() and added new HttpClientHandler loger.</PackageReleaseNotes>
<PackageReleaseNotes>Added signal r incomming and outgoing message logging</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down