-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathConfigure.RequestLogs.cs
34 lines (29 loc) · 1.17 KB
/
Configure.RequestLogs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using ServiceStack.Jobs;
using ServiceStack.Web;
[assembly: HostingStartup(typeof(AiServer.ConfigureRequestLogs))]
namespace AiServer;
public class ConfigureRequestLogs : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) => {
services.AddPlugin(new RequestLogsFeature {
RequestLogger = new SqliteRequestLogger(),
// EnableResponseTracking = true,
EnableRequestBodyTracking = true,
EnableErrorTracking = true
});
services.AddHostedService<RequestLogsHostedService>();
});
}
public class RequestLogsHostedService(ILogger<RequestLogsHostedService> log, IRequestLogger requestLogger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var dbRequestLogger = (SqliteRequestLogger)requestLogger;
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(3));
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
{
dbRequestLogger.Tick(log);
}
}
}