Skip to content

Commit b640ef5

Browse files
committed
fix: use LogContext.PushProperty
1 parent a873e10 commit b640ef5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/GZCTF/Middlewares/PrivilegeAuthentication.cs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Mvc.Filters;
44
using Microsoft.EntityFrameworkCore;
55
using Microsoft.Extensions.Localization;
6+
using Serilog;
67

78
namespace GZCTF.Middlewares;
89

@@ -20,6 +21,8 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
2021
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<RequirePrivilegeAttribute>>();
2122
var dbContext = context.HttpContext.RequestServices.GetRequiredService<AppDbContext>();
2223
var localizer = context.HttpContext.RequestServices.GetRequiredService<IStringLocalizer<Program>>();
24+
var diagnosticContext = context.HttpContext.RequestServices.GetRequiredService<IDiagnosticContext>();
25+
2326
var id = context.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
2427

2528
UserInfo? user = null;
@@ -35,6 +38,10 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
3538
return;
3639
}
3740

41+
diagnosticContext.Set("UserId", user.Id);
42+
diagnosticContext.Set("UserName", user.UserName);
43+
diagnosticContext.Set("IP", context.HttpContext.Connection.RemoteIpAddress);
44+
3845
if (DateTimeOffset.UtcNow - user.LastVisitedUtc > TimeSpan.FromSeconds(5))
3946
{
4047
user.UpdateByHttpContext(context.HttpContext);

src/GZCTF/Utils/LogHelper.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net;
33
using GZCTF.Extensions;
44
using Serilog;
5+
using Serilog.Context;
56
using Serilog.Events;
67
using Serilog.Filters;
78
using Serilog.Sinks.File.Archive;
@@ -16,7 +17,7 @@ public static class LogHelper
1617
const string LogTemplate = "[{@t:yy-MM-dd HH:mm:ss.fff} {@l:u3}] " +
1718
"{Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}: " +
1819
"{@m} {#if Length(Status) > 0}#{Status} <{UserName}>" +
19-
"{#if Length(IP) > 0}@{IP}{#end}{#end}\n{@x}";
20+
"{#if Length(IP) > 0} @ {IP}{#end}{#end}\n{@x}";
2021

2122
const string InitLogTemplate = "[{@t:yy-MM-dd HH:mm:ss.fff} {@l:u3}] {@m}\n{@x}";
2223

@@ -83,7 +84,9 @@ public static void Log<T>(this ILogger<T> logger, string msg, string ip, TaskSta
8384
public static void Log<T>(this ILogger<T> logger, string msg, string uname, string ip, TaskStatus status,
8485
LogLevel? level = null)
8586
{
86-
using (logger.BeginScope("{UserName}{Status}{IP}", uname, status, ip))
87+
using (LogContext.PushProperty("UserName", uname))
88+
using (LogContext.PushProperty("IP", ip))
89+
using (LogContext.PushProperty("Status", status.ToString()))
8790
{
8891
logger.Log(level ?? LogLevel.Information, "{msg:l}", msg);
8992
}
@@ -93,15 +96,15 @@ public static void UseRequestLogging(this WebApplication app) =>
9396
app.UseSerilogRequestLogging(options =>
9497
{
9598
options.MessageTemplate =
96-
"[{StatusCode}] {Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath} @ {RemoteIP}";
99+
"[{StatusCode}] {Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath} @ {IP}";
97100
options.GetLevel = (context, time, ex) =>
98101
context.Response.StatusCode == 204 ? LogEventLevel.Verbose :
99102
time > 10000 && context.Response.StatusCode != 101 ? LogEventLevel.Warning :
100103
context.Response.StatusCode > 499 || ex is not null ? LogEventLevel.Error : LogEventLevel.Debug;
101104

102105
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
103106
{
104-
diagnosticContext.Set("RemoteIP", httpContext.Connection.RemoteIpAddress);
107+
diagnosticContext.Set("IP", httpContext.Connection.RemoteIpAddress);
105108
};
106109
});
107110

0 commit comments

Comments
 (0)