Skip to content

Commit

Permalink
fix: log message in signalr
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Feb 15, 2024
1 parent ccea302 commit 72459ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
18 changes: 5 additions & 13 deletions src/GZCTF/Extensions/DatabaseSinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,14 @@ static LogModel ToLogModel(LogEvent logEvent)
TimeUtc = logEvent.Timestamp.ToUniversalTime(),
Level = logEvent.Level.ToString(),
Message = logEvent.RenderMessage(),
UserName = GetStringValue(userName, "Anonymous"),
Logger = GetStringValue(sourceContext, "Unknown"),
RemoteIP = GetStringValue(ip),
Status = GetStringValue(status),
UserName = LogHelper.GetStringValue(userName, "Anonymous"),
Logger = LogHelper.GetStringValue(sourceContext, "Unknown"),
RemoteIP = LogHelper.GetStringValue(ip),
Status = LogHelper.GetStringValue(status),
Exception = logEvent.Exception?.ToString()
};
}

static string GetStringValue(LogEventPropertyValue? value, string defaultValue = "")
{
if (value is ScalarValue { Value: string rawValue })
return rawValue;
return value?.ToString() ?? defaultValue;
}


async Task WriteToDatabase(CancellationToken token = default)
{
List<LogModel> lockedLogBuffer = [];
Expand Down Expand Up @@ -105,4 +97,4 @@ async Task WriteToDatabase(CancellationToken token = default)
}
catch (TaskCanceledException) { }
}
}
}
12 changes: 8 additions & 4 deletions src/GZCTF/Extensions/SignalRSinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,26 @@ public void Emit(LogEvent logEvent)

_hubContext ??= serviceProvider.GetRequiredService<IHubContext<AdminHub, IAdminClient>>();

logEvent.Properties.TryGetValue("UserName", out LogEventPropertyValue? userName);
logEvent.Properties.TryGetValue("IP", out LogEventPropertyValue? ip);
logEvent.Properties.TryGetValue("Status", out LogEventPropertyValue? status);

try
{
_hubContext.Clients.All.ReceivedLog(
new LogMessageModel
{
Time = logEvent.Timestamp,
Level = logEvent.Level.ToString(),
UserName = logEvent.Properties["UserName"].ToString()[1..^1],
IP = logEvent.Properties["IP"].ToString()[1..^1],
Msg = logEvent.RenderMessage(),
Status = logEvent.Properties["Status"].ToString()
UserName = LogHelper.GetStringValue(userName, "Anonymous"),
IP = LogHelper.GetStringValue(ip),
Status = LogHelper.GetStringValue(status),
}).Wait();
}
catch
{
// ignored
}
}
}
}
7 changes: 7 additions & 0 deletions src/GZCTF/Utils/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,11 @@ public static ILogger GetLogger(IConfiguration configuration, IServiceProvider s
.WriteTo.Database(serviceProvider)
.WriteTo.SignalR(serviceProvider)
.CreateLogger();

public static string GetStringValue(LogEventPropertyValue? value, string defaultValue = "")
{
if (value is ScalarValue { Value: string rawValue })
return rawValue;
return value?.ToString() ?? defaultValue;
}
}

0 comments on commit 72459ec

Please sign in to comment.