diff --git a/MyApp/Configure.RequestLogs.cs b/MyApp/Configure.RequestLogs.cs new file mode 100644 index 0000000..57cef3b --- /dev/null +++ b/MyApp/Configure.RequestLogs.cs @@ -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(); + }); +} + +public class RequestLogsHostedService(ILogger 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); + } + } +}