Skip to content

Commit 17c3825

Browse files
committed
feat: 新增 HTTP 日誌記錄配置及優化路由處理邏輯
1 parent dded2b4 commit 17c3825

14 files changed

Lines changed: 252 additions & 222 deletions

File tree

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
1+
using Microsoft.AspNetCore.HttpLogging;
2+
using Netcorext.Auth.Authentication.Settings;
3+
using Netcorext.Configuration.ConfigSections;
4+
using Netcorext.Extensions.Linq;
5+
16
namespace Netcorext.Auth.Authentication.InjectionConfigs;
27

38
[Injection]
49
public class MvcConfig
510
{
6-
public MvcConfig(IServiceCollection services)
11+
public MvcConfig(IServiceCollection services, IConfiguration configuration)
712
{
13+
var config = configuration.Get<ConfigSettings>()!;
14+
15+
services.AddHttpLogging(options =>
16+
{
17+
options.LoggingFields = config.HttpLoggingOptions.LoggingFields switch
18+
{
19+
HttpLoggingSection.HttpLoggingFields.None => HttpLoggingFields.None,
20+
HttpLoggingSection.HttpLoggingFields.RequestPath => HttpLoggingFields.RequestPath,
21+
HttpLoggingSection.HttpLoggingFields.RequestQuery => HttpLoggingFields.RequestQuery,
22+
HttpLoggingSection.HttpLoggingFields.RequestProtocol => HttpLoggingFields.RequestProtocol,
23+
HttpLoggingSection.HttpLoggingFields.RequestMethod => HttpLoggingFields.RequestMethod,
24+
HttpLoggingSection.HttpLoggingFields.RequestScheme => HttpLoggingFields.RequestScheme,
25+
HttpLoggingSection.HttpLoggingFields.ResponseStatusCode => HttpLoggingFields.ResponseStatusCode,
26+
HttpLoggingSection.HttpLoggingFields.RequestHeaders => HttpLoggingFields.RequestHeaders,
27+
HttpLoggingSection.HttpLoggingFields.ResponseHeaders => HttpLoggingFields.ResponseHeaders,
28+
HttpLoggingSection.HttpLoggingFields.RequestTrailers => HttpLoggingFields.RequestTrailers,
29+
HttpLoggingSection.HttpLoggingFields.ResponseTrailers => HttpLoggingFields.ResponseTrailers,
30+
HttpLoggingSection.HttpLoggingFields.RequestBody => HttpLoggingFields.RequestBody,
31+
HttpLoggingSection.HttpLoggingFields.ResponseBody => HttpLoggingFields.ResponseBody,
32+
HttpLoggingSection.HttpLoggingFields.RequestProperties => HttpLoggingFields.RequestProperties,
33+
HttpLoggingSection.HttpLoggingFields.RequestPropertiesAndHeaders => HttpLoggingFields.RequestPropertiesAndHeaders,
34+
HttpLoggingSection.HttpLoggingFields.ResponsePropertiesAndHeaders => HttpLoggingFields.ResponsePropertiesAndHeaders,
35+
HttpLoggingSection.HttpLoggingFields.Request => HttpLoggingFields.Request,
36+
HttpLoggingSection.HttpLoggingFields.Response => HttpLoggingFields.Response,
37+
HttpLoggingSection.HttpLoggingFields.All => HttpLoggingFields.All,
38+
_ => HttpLoggingFields.RequestPropertiesAndHeaders | HttpLoggingFields.ResponsePropertiesAndHeaders
39+
};
40+
41+
config.HttpLoggingOptions.RequestHeaders.ForEach(t => options.RequestHeaders.Add(t));
42+
config.HttpLoggingOptions.ResponseHeaders.ForEach(t => options.ResponseHeaders.Add(t));
43+
44+
options.RequestBodyLogLimit = config.HttpLoggingOptions.RequestBodyLogLimit;
45+
options.ResponseBodyLogLimit = config.HttpLoggingOptions.ResponseBodyLogLimit;
46+
});
47+
848
services.AddCors();
949
}
1050
}

src/Netcorext.Auth.Authentication/InjectionConfigs/ServiceConfig.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ public ServiceConfig(IServiceCollection services, IConfiguration configuration)
1212
var cfg = configuration.Get<ConfigSettings>()!;
1313

1414
services.AddMediator()
15-
.AddRedisQueuing((provider, options) =>
15+
.AddRedisQueuing((_, options) =>
1616
{
17-
var cfg = provider.GetRequiredService<IOptions<ConfigSettings>>().Value;
1817
options.ConnectionString = cfg.Connections.Redis.GetDefault().Connection;
1918
})
2019
.AddLoggingPipeline()

src/Netcorext.Auth.Authentication/Services/Route/Queries/GetRoute/GetRouteHandler.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.EntityFrameworkCore;
33
using Netcorext.Contracts;
44
using Netcorext.EntityFramework.UserIdentityPattern;
5+
using Netcorext.Extensions.Commons;
56
using Netcorext.Extensions.Linq;
67
using Netcorext.Mediator;
78
using Yarp.ReverseProxy.Forwarder;
@@ -21,12 +22,10 @@ public GetRouteHandler(DatabaseContextAdapter context)
2122
{
2223
var ds = _context.Set<Domain.Entities.RouteGroup>();
2324

24-
Expression<Func<Domain.Entities.RouteGroup, bool>> predicate = p => request.GroupIds == null;
25+
Expression<Func<Domain.Entities.RouteGroup, bool>> predicate = p => true;
2526

26-
if (request.GroupIds != null && request.GroupIds.Any())
27-
{
28-
predicate = request.GroupIds.Aggregate(predicate, (current, id) => current.Or(p => p.Id == id));
29-
}
27+
if (!request.GroupIds.IsEmpty())
28+
predicate = predicate.And(t => request.GroupIds.Contains(t.Id));
3029

3130
var queryEntities = ds.Where(predicate)
3231
.AsNoTracking();
@@ -61,10 +60,9 @@ public GetRouteHandler(DatabaseContextAdapter context)
6160
Value = t3.Value
6261
})
6362
})
64-
});
63+
})
64+
.ToArray();
6565

66-
return Task.FromResult(!content.Any()
67-
? Result<IEnumerable<Models.RouteGroup>>.Success
68-
: Result<IEnumerable<Models.RouteGroup>>.Success.Clone(content.ToArray()));
66+
return Task.FromResult(Result<IEnumerable<Models.RouteGroup>>.Success.Clone(content));
6967
}
70-
}
68+
}

src/Netcorext.Auth.Authentication/Workers/Runners/BlockedIpRunner.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Netcorext.Auth.Authentication.Services.Blocked.Queries;
55
using Netcorext.Auth.Authentication.Settings;
66
using Netcorext.Contracts;
7+
using Netcorext.Extensions.Commons;
78
using Netcorext.Extensions.Linq;
89
using Netcorext.Mediator;
910
using Netcorext.Serialization;
@@ -64,32 +65,29 @@ private async Task UpdateBlockedIpAsync(string? ids, CancellationToken cancellat
6465
using var scope = _serviceProvider.CreateScope();
6566
var dispatcher = scope.ServiceProvider.GetRequiredService<IDispatcher>();
6667

67-
var reqIds = ids == null ? null : _serializer.Deserialize<long[]>(ids);
68-
69-
var result = await dispatcher.SendAsync(new GetBlockedIp
70-
{
71-
Ids = reqIds
72-
}, cancellationToken);
73-
74-
if (result.Content == null || result.Code != Result.Success) return;
68+
var reqIds = ids.IsEmpty() ? null : _serializer.Deserialize<long[]>(ids);
7569

7670
var cacheBlockedIp = _cache.Get<Dictionary<long, Services.Blocked.Queries.Models.BlockedIp>>(ConfigSettings.CACHE_BLOCKED_IP) ?? new Dictionary<long, Services.Blocked.Queries.Models.BlockedIp>();
7771

78-
if (reqIds != null && reqIds.Any())
79-
{
80-
var rules = cacheBlockedIp.Where(t => reqIds.Contains(t.Value.Id))
81-
.ToArray();
72+
if (reqIds.IsEmpty())
73+
cacheBlockedIp.Clear();
74+
else
75+
reqIds.ForEach(t => cacheBlockedIp.Remove(t));
8276

83-
rules.ForEach(t => cacheBlockedIp.Remove(t.Key));
84-
}
77+
var result = await dispatcher.SendAsync(new GetBlockedIp
78+
{
79+
Ids = reqIds.IsEmpty() ? null : reqIds
80+
}, cancellationToken);
8581

86-
foreach (var i in result.Content)
82+
if (result.Code == Result.Success && !result.Content.IsEmpty())
8783
{
88-
var id = i.Id;
89-
90-
if (cacheBlockedIp.TryAdd(id, i)) continue;
84+
result.Content.ForEach(t =>
85+
{
86+
if (cacheBlockedIp.TryAdd(t.Id, t))
87+
return;
9188

92-
cacheBlockedIp[id] = i;
89+
cacheBlockedIp[t.Id] = t;
90+
});
9391
}
9492

9593
_cache.Set(ConfigSettings.CACHE_BLOCKED_IP, cacheBlockedIp, _cacheEntryOptions);

src/Netcorext.Auth.Authentication/Workers/Runners/ClientRunner.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.Extensions.Options;
44
using Netcorext.Auth.Authentication.Services.Client.Queries;
55
using Netcorext.Auth.Authentication.Settings;
6+
using Netcorext.Contracts;
7+
using Netcorext.Extensions.Commons;
68
using Netcorext.Extensions.Linq;
79
using Netcorext.Mediator;
810
using Netcorext.Serialization;
@@ -67,31 +69,29 @@ private async Task UpdateClientAsync(string? ids, CancellationToken cancellation
6769
using var scope = _serviceProvider.CreateScope();
6870
var dispatcher = scope.ServiceProvider.GetRequiredService<IDispatcher>();
6971

70-
var reqIds = ids == null ? null : _serializer.Deserialize<long[]>(ids);
72+
var reqIds = ids.IsEmpty() ? null : _serializer.Deserialize<long[]>(ids);
73+
74+
var cacheClient = _cache.Get<Dictionary<long, Netcorext.Auth.Authentication.Services.Client.Queries.Models.Client>>(ConfigSettings.CACHE_CLIENT) ?? new Dictionary<long, Netcorext.Auth.Authentication.Services.Client.Queries.Models.Client>();
75+
76+
if (reqIds.IsEmpty())
77+
cacheClient.Clear();
78+
else
79+
reqIds.ForEach(t => cacheClient.Remove(t));
7180

7281
var result = await dispatcher.SendAsync(new GetClient
7382
{
74-
Ids = reqIds
83+
Ids = reqIds.IsEmpty() ? null : reqIds
7584
}, cancellationToken);
7685

77-
var cacheClient = _cache.Get<Dictionary<long, Netcorext.Auth.Authentication.Services.Client.Queries.Models.Client>>(ConfigSettings.CACHE_CLIENT) ?? new Dictionary<long, Netcorext.Auth.Authentication.Services.Client.Queries.Models.Client>();
78-
79-
if (reqIds == null || !reqIds.Any())
86+
if (result.Code == Result.Success && !result.Content.IsEmpty())
8087
{
81-
cacheClient.Clear();
88+
result.Content.ForEach(t =>
89+
{
90+
if (cacheClient.TryAdd(t.Id, t))
91+
return;
8292

83-
if (result.Content != null && result.Content.Any())
84-
result.Content.ForEach(t => cacheClient.TryAdd(t.Id, t));
85-
}
86-
else if (result.Content == null || !result.Content.Any())
87-
{
88-
reqIds.ForEach(t => cacheClient.Remove(t));
89-
}
90-
else
91-
{
92-
var diffIds = reqIds.Except(result.Content.Select(t => t.Id)).ToArray();
93-
diffIds.ForEach(t => cacheClient.Remove(t));
94-
result.Content.ForEach(t => cacheClient.Add(t.Id, t));
93+
cacheClient[t.Id] = t;
94+
});
9595
}
9696

9797
_cache.Set(ConfigSettings.CACHE_CLIENT, cacheClient, _cacheEntryOptions);

src/Netcorext.Auth.Authentication/Workers/Runners/MaintainRunner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Netcorext.Auth.Authentication.Services.Maintenance.Queries;
55
using Netcorext.Auth.Authentication.Settings;
66
using Netcorext.Contracts;
7+
using Netcorext.Extensions.Commons;
78
using Netcorext.Mediator;
89
using Netcorext.Serialization;
910
using Netcorext.Worker;
@@ -64,7 +65,7 @@ private async Task UpdateMaintainAsync(string? data, CancellationToken cancellat
6465
var dispatcher = scope.ServiceProvider.GetRequiredService<IDispatcher>();
6566
var result = await dispatcher.SendAsync(new GetMaintain(), cancellationToken);
6667

67-
if (result.Content == null || result.Code != Result.Success) return;
68+
if (result.Content.IsEmpty() || result.Code != Result.Success) return;
6869

6970
_cache.Set($"{ConfigSettings.CACHE_MAINTAIN}", result.Content, _cacheEntryOptions);
7071
}

src/Netcorext.Auth.Authentication/Workers/Runners/PermissionRunner.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Netcorext.Auth.Authentication.Services.Permission.Queries;
55
using Netcorext.Auth.Authentication.Settings;
66
using Netcorext.Contracts;
7+
using Netcorext.Extensions.Commons;
78
using Netcorext.Extensions.Linq;
89
using Netcorext.Mediator;
910
using Netcorext.Serialization;
@@ -64,32 +65,36 @@ private async Task UpdatePermissionAsync(string? ids, CancellationToken cancella
6465
using var scope = _serviceProvider.CreateScope();
6566
var dispatcher = scope.ServiceProvider.GetRequiredService<IDispatcher>();
6667

67-
var reqIds = ids == null ? null : _serializer.Deserialize<long[]>(ids);
68-
69-
var result = await dispatcher.SendAsync(new GetPermission
70-
{
71-
Ids = reqIds
72-
}, cancellationToken);
73-
74-
if (result.Content == null || result.Code != Result.Success) return;
68+
var reqIds = ids.IsEmpty() ? null : _serializer.Deserialize<long[]>(ids);
7569

7670
var cachePermissionRule = _cache.Get<Dictionary<long, Services.Permission.Queries.Models.PermissionRule>>(ConfigSettings.CACHE_PERMISSION_RULE) ?? new Dictionary<long, Services.Permission.Queries.Models.PermissionRule>();
7771

78-
if (reqIds != null && reqIds.Any())
72+
if (reqIds.IsEmpty())
73+
{
74+
cachePermissionRule.Clear();
75+
}
76+
else
7977
{
8078
var rules = cachePermissionRule.Where(t => reqIds.Contains(t.Value.PermissionId))
8179
.ToArray();
8280

8381
rules.ForEach(t => cachePermissionRule.Remove(t.Key));
8482
}
8583

86-
foreach (var i in result.Content)
87-
{
88-
var id = i.Id;
84+
var result = await dispatcher.SendAsync(new GetPermission
85+
{
86+
Ids = reqIds.IsEmpty() ? null : reqIds
87+
}, cancellationToken);
8988

90-
if (cachePermissionRule.TryAdd(id, i)) continue;
89+
if (result.Code == Result.Success && !result.Content.IsEmpty())
90+
{
91+
result.Content.ForEach(t =>
92+
{
93+
if (cachePermissionRule.TryAdd(t.Id, t))
94+
return;
9195

92-
cachePermissionRule[id] = i;
96+
cachePermissionRule[t.Id] = t;
97+
});
9398
}
9499

95100
_cache.Set(ConfigSettings.CACHE_PERMISSION_RULE, cachePermissionRule, _cacheEntryOptions);

0 commit comments

Comments
 (0)