Created
September 9, 2024 07:53
-
-
Save gistlyn/55d313526a882d1f95c995284448a1ac to your computer and use it in GitHub Desktop.
sqlitelogs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet add package ServiceStack.Jobs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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