Skip to content

Commit b705de4

Browse files
committed
logging signalr added
1 parent 7248926 commit b705de4

File tree

3 files changed

+102
-19
lines changed

3 files changed

+102
-19
lines changed

src/SharedKernel/Extensions/SignalRExtensions.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,41 @@
22
using Microsoft.AspNetCore.SignalR;
33
using Microsoft.Extensions.DependencyInjection;
44
using ResponseCrafter.ExceptionHandlers.SignalR;
5+
using SharedKernel.Logging;
56
using StackExchange.Redis;
67

78
namespace SharedKernel.Extensions;
89

910
public static class SignalRExtensions
1011
{
11-
1212
public static WebApplicationBuilder AddSignalR(this WebApplicationBuilder builder)
1313
{
14-
builder
15-
.Services
16-
.AddSignalR(o => o.AddFilter<SignalRExceptionFilter>())
17-
.AddMessagePackProtocol();
18-
14+
builder.AddSignalRWithFiltersAndMessagePack();
1915
return builder;
2016
}
21-
22-
public static WebApplicationBuilder AddDistributedSignalR(this WebApplicationBuilder builder, string redisChannelName)
17+
18+
public static WebApplicationBuilder AddDistributedSignalR(this WebApplicationBuilder builder,
19+
string redisChannelName)
2320
{
24-
builder
25-
.Services
26-
.AddSignalR(o => o.AddFilter<SignalRExceptionFilter>())
27-
.AddMessagePackProtocol()
28-
.AddStackExchangeRedis(builder.Configuration.GetRedisUrl(),
29-
options =>
30-
{
31-
options.Configuration.ChannelPrefix = RedisChannel.Literal("FinHub:SignalR:");
32-
});
21+
builder.AddSignalRWithFiltersAndMessagePack()
22+
.AddStackExchangeRedis(builder.Configuration.GetRedisUrl(),
23+
options =>
24+
{
25+
options.Configuration.ChannelPrefix = RedisChannel.Literal("FinHub:SignalR:");
26+
});
3327

3428

3529
return builder;
3630
}
31+
32+
private static ISignalRServerBuilder AddSignalRWithFiltersAndMessagePack(this WebApplicationBuilder builder)
33+
{
34+
return builder.Services
35+
.AddSignalR(o =>
36+
{
37+
o.AddFilter<SignalRExceptionFilter>();
38+
o.AddFilter<SignalRLoggingHubFilter>();
39+
})
40+
.AddMessagePackProtocol();
41+
}
3742
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Diagnostics;
2+
using System.Text.Json;
3+
using Microsoft.AspNetCore.SignalR;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace SharedKernel.Logging;
7+
8+
internal sealed class SignalRLoggingHubFilter(ILogger<SignalRLoggingHubFilter> logger) : IHubFilter
9+
{
10+
public async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext,
11+
Func<HubInvocationContext, ValueTask<object?>> next)
12+
{
13+
var start = Stopwatch.GetTimestamp();
14+
15+
// Basic context info
16+
var hubName = invocationContext.Hub.GetType()
17+
.Name;
18+
var connectionId = invocationContext.Context.ConnectionId;
19+
var userId = invocationContext.Context.UserIdentifier;
20+
var methodName = invocationContext.HubMethodName;
21+
22+
// Redact arguments
23+
var serializedArgs = JsonSerializer.Serialize(invocationContext.HubMethodArguments);
24+
var redactedArgs = RedactionHelper.ParseAndRedactJson(serializedArgs);
25+
26+
object? result = null;
27+
Exception? exception = null;
28+
29+
try
30+
{
31+
// Invoke the actual hub method
32+
result = await next(invocationContext);
33+
}
34+
catch (Exception ex)
35+
{
36+
exception = ex;
37+
}
38+
39+
var elapsedMs = Stopwatch.GetElapsedTime(start)
40+
.TotalMilliseconds;
41+
42+
if (exception is not null)
43+
{
44+
logger.LogError(exception,
45+
"[SignalR] Hub {HubName}, ConnId {ConnectionId}, UserId {UserId} - Method {MethodName} threw an exception after {ElapsedMs}ms. " +
46+
"Inbound Args: {Args}",
47+
hubName,
48+
connectionId,
49+
userId,
50+
methodName,
51+
elapsedMs,
52+
redactedArgs);
53+
throw exception;
54+
}
55+
56+
// Redact return value, if any
57+
var redactedResult = string.Empty;
58+
if (result is not null)
59+
{
60+
var serializedResult = JsonSerializer.Serialize(result);
61+
var redactedObj = RedactionHelper.ParseAndRedactJson(serializedResult);
62+
redactedResult = JsonSerializer.Serialize(redactedObj);
63+
}
64+
65+
logger.LogInformation(
66+
"[SignalR] Hub {HubName}, ConnId {ConnectionId}, UserId {UserId} - Method {MethodName} completed in {ElapsedMs}ms. " +
67+
"Inbound Args: {Args}, Outbound Result: {Result}",
68+
hubName,
69+
connectionId,
70+
userId,
71+
methodName,
72+
elapsedMs,
73+
redactedArgs,
74+
redactedResult);
75+
76+
return result;
77+
}
78+
}

src/SharedKernel/SharedKernel.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.1.0</Version>
11+
<Version>1.1.1</Version>
1212
<PackageId>Pandatech.SharedKernel</PackageId>
1313
<Title>Pandatech Shared Kernel Library</Title>
1414
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
1515
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
17-
<PackageReleaseNotes>app.UseRequestResponseLogging() changed to app.UseRequestLogging() and added new HttpClientHandler loger.</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added signal r incomming and outgoing message logging</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)