Skip to content

Commit

Permalink
Add SqliteRequestLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Sep 9, 2024
1 parent 7757b9b commit 927c356
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions MyApp/Configure.RequestLogs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ServiceStack.Jobs;
using ServiceStack.Web;

[assembly: HostingStartup(typeof(MyApp.ConfigureRequestLogs))]

namespace MyApp;

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);
}
}
}

0 comments on commit 927c356

Please sign in to comment.