Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Created September 9, 2024 07:53
Show Gist options
  • Save gistlyn/55d313526a882d1f95c995284448a1ac to your computer and use it in GitHub Desktop.
Save gistlyn/55d313526a882d1f95c995284448a1ac to your computer and use it in GitHub Desktop.
sqlitelogs
dotnet add package ServiceStack.Jobs
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment