Skip to content

Commit

Permalink
fix: use pattern matching to get raw sting value
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Feb 6, 2024
1 parent 70e6cd6 commit c934718
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/GZCTF/Extensions/DatabaseSinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@ static LogModel ToLogModel(LogEvent logEvent)
TimeUtc = logEvent.Timestamp.ToUniversalTime(),
Level = logEvent.Level.ToString(),
Message = logEvent.RenderMessage(),
UserName = userName?.ToString()[1..^1] ?? "Anonymous",
Logger = sourceContext?.ToString()[1..^1] ?? "Unknown",
RemoteIP = ip?.ToString()[1..^1] ?? "",
Status = status?.ToString() ?? "",
UserName = GetStringValue(userName, "Anonymous"),
Logger = GetStringValue(sourceContext, "Unknown"),
RemoteIP = GetStringValue(ip),
Status = 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
2 changes: 1 addition & 1 deletion src/GZCTF/Utils/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static void Log<T>(this ILogger<T> logger, string msg, string uname, stri
{
using (LogContext.PushProperty("UserName", uname))
using (LogContext.PushProperty("IP", ip))
using (LogContext.PushProperty("Status", status.ToString()))
using (LogContext.PushProperty("Status", status))
{
logger.Log(level ?? LogLevel.Information, "{msg:l}", msg);
}
Expand Down

0 comments on commit c934718

Please sign in to comment.