Skip to content

Commit b77fcc0

Browse files
committed
Enable Request Logs
1 parent 5068ec1 commit b77fcc0

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
55

66
# Custom
7+
*.xml
8+
requests/
79
dist/
810
# wwwroot/
911
coverage/

TechStacks/Configure.Auth.cs

+79
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.Extensions.DependencyInjection.Extensions;
13
using ServiceStack.Auth;
24
using ServiceStack;
35
using ServiceStack.Html;
@@ -59,5 +61,82 @@ public void Configure(IWebHostBuilder builder) => builder
5961
});
6062

6163
})));
64+
65+
services.TryAddScoped(typeof(RoleManager<IdentityRole>), CreateRoleManagerAdapter<ApplicationRole>);
6266
});
67+
68+
public static RoleManager<IdentityRole> CreateRoleManagerAdapter<TRole>(IServiceProvider services) where TRole : class
69+
{
70+
//var nativeRoleManager = services.GetRequiredService<RoleManager<TRole>>();
71+
var adapter = new RoleManager<IdentityRole>(
72+
CreateRoleStoreAdapter<TRole>(services),
73+
[],
74+
services.GetRequiredService<ILookupNormalizer>(),
75+
services.GetRequiredService<IdentityErrorDescriber>(),
76+
new Logger<RoleManager<IdentityRole>>(services.GetRequiredService<ILoggerFactory>())
77+
);
78+
return adapter;
79+
}
80+
81+
public static IRoleStore<IdentityRole> CreateRoleStoreAdapter<TRole>(IServiceProvider services) where TRole : class
82+
{
83+
return new AdapterRoleStore<TRole>(services.GetRequiredService<IRoleStore<TRole>>());
84+
}
85+
86+
class AdapterRoleStore<TRole>(IRoleStore<TRole> store) : IRoleStore<IdentityRole> where TRole : class
87+
{
88+
public void Dispose()
89+
{
90+
}
91+
92+
public Task<IdentityResult> CreateAsync(IdentityRole role, CancellationToken cancellationToken)
93+
{
94+
throw new NotImplementedException();
95+
}
96+
97+
public Task<IdentityResult> UpdateAsync(IdentityRole role, CancellationToken cancellationToken)
98+
{
99+
throw new NotImplementedException();
100+
}
101+
102+
public Task<IdentityResult> DeleteAsync(IdentityRole role, CancellationToken cancellationToken)
103+
{
104+
throw new NotImplementedException();
105+
}
106+
107+
public Task<string> GetRoleIdAsync(IdentityRole role, CancellationToken cancellationToken)
108+
{
109+
throw new NotImplementedException();
110+
}
111+
112+
public Task<string?> GetRoleNameAsync(IdentityRole role, CancellationToken cancellationToken)
113+
{
114+
throw new NotImplementedException();
115+
}
116+
117+
public Task SetRoleNameAsync(IdentityRole role, string? roleName, CancellationToken cancellationToken)
118+
{
119+
throw new NotImplementedException();
120+
}
121+
122+
public Task<string?> GetNormalizedRoleNameAsync(IdentityRole role, CancellationToken cancellationToken)
123+
{
124+
throw new NotImplementedException();
125+
}
126+
127+
public Task SetNormalizedRoleNameAsync(IdentityRole role, string? normalizedName, CancellationToken cancellationToken)
128+
{
129+
throw new NotImplementedException();
130+
}
131+
132+
public Task<IdentityRole?> FindByIdAsync(string roleId, CancellationToken cancellationToken)
133+
{
134+
throw new NotImplementedException();
135+
}
136+
137+
public Task<IdentityRole?> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
138+
{
139+
throw new NotImplementedException();
140+
}
141+
}
63142
}

TechStacks/Configure.RequestLogs.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using ServiceStack;
2+
using ServiceStack.Jobs;
3+
using ServiceStack.Web;
4+
5+
[assembly: HostingStartup(typeof(AiServer.ConfigureRequestLogs))]
6+
7+
namespace AiServer;
8+
9+
public class ConfigureRequestLogs : IHostingStartup
10+
{
11+
public void Configure(IWebHostBuilder builder) => builder
12+
.ConfigureServices((context, services) => {
13+
14+
services.AddPlugin(new RequestLogsFeature {
15+
RequestLogger = new SqliteRequestLogger(),
16+
EnableResponseTracking = true,
17+
EnableRequestBodyTracking = true,
18+
EnableErrorTracking = true
19+
});
20+
services.AddHostedService<RequestLogsHostedService>();
21+
});
22+
}
23+
24+
public class RequestLogsHostedService(ILogger<RequestLogsHostedService> log, IRequestLogger requestLogger) : BackgroundService
25+
{
26+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
27+
{
28+
var dbRequestLogger = (SqliteRequestLogger)requestLogger;
29+
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(3));
30+
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
31+
{
32+
dbRequestLogger.Tick(log);
33+
}
34+
}
35+
}

TechStacks/TechStacks.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
</ItemGroup>
1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.*" />
19-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.*" />
19+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.*" />
2020
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.*" />
21-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.*" />
21+
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.*" />
2222
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="8.*" />
2323
<PackageReference Include="Markdig" Version="0.37.*" />
2424

@@ -28,6 +28,7 @@
2828
<PackageReference Include="ServiceStack.OrmLite.PostgreSQL" Version="8.*"/>
2929
<PackageReference Include="ServiceStack.Extensions" Version="8.*"/>
3030
<PackageReference Include="ServiceStack.AspNetCore.OpenApi" Version="8.*"/>
31+
<PackageReference Include="ServiceStack.Jobs" Version="8.*"/>
3132

3233
<!-- <ProjectReference Include="..\..\..\ServiceStack\ServiceStack\ServiceStack\src\ServiceStack.Api.OpenApi\ServiceStack.Api.OpenApi.csproj"/>-->
3334
<!-- <ProjectReference Include="..\..\..\ServiceStack\ServiceStack\ServiceStack.OrmLite\src\ServiceStack.OrmLite.PostgreSQL\ServiceStack.OrmLite.PostgreSQL.csproj"/>-->

0 commit comments

Comments
 (0)