Skip to content

Commit d3442a2

Browse files
committed
Create Configure.RequestLogs.cs
1 parent 563ddbd commit d3442a2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

MyApp/Configure.RequestLogs.cs

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

0 commit comments

Comments
 (0)