serilog-contrib/serilog-sinks-azuretablestorage

Logs called from middleware aren't being uploaded.

ttugates opened this issue · 4 comments

I have config such as the following:

Log.Logger = new LoggerConfiguration()
	.MinimumLevel.Warning()
	.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
	.MinimumLevel.Override("System", LogEventLevel.Warning)
	.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Warning)
	.Enrich.FromLogContext()
	.WriteTo.AzureTableStorage(azureStorage, LogEventLevel.Warning, null, "ProductionLogs", true, new TimeSpan(0, 0, 1), 10, new AzureStorageLogKeyGenerator())
	.CreateLogger();

And middleware like this:

public class SerilogMiddleware
{
	readonly RequestDelegate _next;

	public SerilogMiddleware(RequestDelegate next)
	{
		if (next == null) throw new ArgumentNullException(nameof(next));
		_next = next;
	}

	public async Task Invoke(HttpContext httpContext)
	{
		if (httpContext == null) throw new ArgumentNullException(nameof(httpContext));
		
		LogContext.PushProperty("User", httpContext.GetEmailOfContext());
		... Etc.
		 
		try
		{
			await _next(httpContext);
		}
		catch (Exception ex)
		{                   
			Log.Error(ex, "Unhandeled Exception");  // <- Hit or miss on getting this log. 
                        // But always shows in console.

			// await Task.Delay(100); <- If I un-comment this line..  
                        // I get the logs in Azure Table Storage. 

			httpContext.Response.StatusCode = 500;
		}				
	}
}

I believe this is a bug, but perhaps I am implementing something incorrectly. I would love to be able to await Log.Error somehow. Either way, how do I address cause? And how do I configure such that when saving to Azure Table Storage Fails, it produces a log in and of itself?

~Thx

Hi @ttugates - is your application exiting after the logging statement, without calling Log.CloseAndFlush()?

Eek, it sure is! Sry for presumption it a bug and thanks for quick response!

For anyone else,

I had an additional issue that caused Logs to inadvertently not record in my IKeyGenerator Implementation.

In an attempt to get the LogEventPropertyValue from LogEvent, I was causing an error. For some reason even when I wrapped it in a try catch, it was never being caught.

All the same, found it by reduction..

Great, glad to hear you've figured it out 👍