Serilog and Seq works fine when I log from WinForm/web application. I am facing problem only when I am using console application. Without writing Log.CloseAndFlush() it is not working. Following is my LoggerConfiguration

Log.Logger = new LoggerConfiguration() .WriteTo.Seq("") .CreateLogger(); 

Is there any way to log without invoking Log.CloseAndFlush() so that it works with seq, serilog in console application.

1

2 Answers

Log.CloseAndFlush() only needs to be called once in a console application, before the application exits. It's a "shutdown" method that ensures any buffered events are processed before the application exits.

The Serilog Lifecycle of Loggers documentation has some more detail on CloseAndFlush().

7

Just tried to understand why Dispose() or CloseAndFlush() not works for AWS Lambda function. And looks like, that Dispose/CloseAndFlush has no guarantee, that all buffered events will be sent during calling this method. Sic!

Here we can see confirmation of such behaviour:

public async Task<string> FunctionHandler(JObject input, ILambdaContext context) { var logger = LogService.GetInstance(); //wrapped Logger instance try { throw new ApplicationException("lambda test error"); } catch (Exception e) { Console.WriteLine(e); logger.Error(e, "AWS_Lambda call failed"); } logger.Dispose(); // here CloseAndFlush or dispose calling (tried both variants) Thread.Sleep(3000); //not work without it return input.ToString().ToUpper(); } 

So, you can just call Thread.Sleep(3000) to hold 2-3 seconds that ... MAYBE will send all logs to your sinks. Not an option for AWS Lambdas of course, so if someone will find a working solution, put it here, please. Thanks!

Possible workaround : Just one solution see here : making this thread delay in case during request/lifecycle you collected some logs in general, so in my case I just set bool flag to true in case of any logging and then checking it before lambda will be finished, if it is true - making thread sleep to allow logs be pushed.Weird, but works well... And of course you need to control that it should happens only for exceptions or other words - rare.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.