I have a requirement where I would like to write logs to a file if database logging failed. The idea here is to again insert those records back to the database from the file by running some batch.
I am using a middleware approach to log request/response/exceptions to the database.
public async Task InvokeAsync(HttpContext context)
{
//execute request and get response
try{
_logger.LogDebug("{database logging template}", log.Request, log.Response, log.Exception);
}
catch(Exception ex){
//Log to file ---> _logger.LogDebug();
}
}
My serilog configuration looks like this
.WriteTo.MSSqlServer(
connectionString: config.GetConnectionString("DB"),
sinkOptions: new SinkOptions { TableName = "LogTable", AutoCreateSqlTable = true },
columnOptions: columnOptions));
I am not sure without blocking the HTTP request, how to check if _logger.LogDebug is successfully executed and if not, proceed in the catch to write the logs in a file.
I can use .AuditTo in serilog but that will make the logging synchronous and hence will block the HTTP response.
Any suggestions?