Last active
October 12, 2025 08:35
-
-
Save gistlyn/d2006635254da4969427cac644566d1e to your computer and use it in GitHub Desktop.
db-requestlogs
This file contains hidden or 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.Server |
This file contains hidden or 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.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 DbRequestLogger { | |
| // NamedConnection = "<alternative db>" | |
| }, | |
| EnableResponseTracking = true, | |
| EnableRequestBodyTracking = true, | |
| EnableErrorTracking = true | |
| }); | |
| services.AddHostedService<RequestLogsHostedService>(); | |
| if (context.HostingEnvironment.IsDevelopment()) | |
| { | |
| services.AddPlugin(new ProfilingFeature()); | |
| } | |
| }); | |
| } | |
| public class RequestLogsHostedService(ILogger<RequestLogsHostedService> log, IRequestLogger requestLogger) : BackgroundService | |
| { | |
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
| { | |
| using var timer = new PeriodicTimer(TimeSpan.FromSeconds(3)); | |
| if (requestLogger is IRequireAnalytics logger) | |
| { | |
| while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken)) | |
| { | |
| await logger.TickAsync(log, stoppingToken); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment