16

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("http://localhost:5341")
                .CreateLogger();

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

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
Hasibul
  • 569
  • 2
  • 6
  • 21
  • Very similar issue [here](https://stackoverflow.com/q/46260308/1698987) . Reading Nicholas link explains it – Noctis Sep 17 '17 at 05:21

2 Answers2

24

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().

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • 1
    Hi Nicholas, Thank you very much for your help. I cannot use disposable instance of Logger as I am using LibLog. On the other hand, I am not going to exit console application because I have to make live console application as it listen a queue and process. Is there any way to log message without exiting the app and using liblog. – Hasibul Sep 17 '17 at 14:29
  • 1
    Events will only be buffered for up to two seconds, so you should see everything if the app is kept running. `LibLog` only provides an abstraction for libraries - in the console app itself, you'll need to set up and tear down Serilog using Serilog's APIs anyway. HTH! – Nicholas Blumhardt Sep 18 '17 at 00:04
  • Thank you very much. It is working. Yes, it logs after 2 seconds and it is OK for me. – Hasibul Sep 20 '17 at 10:29
  • This is interesting - [here](https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers#without-using-log) it says that Log.CloseAndFlush() is not needed when you create your own logging configuration. What's the correct approach? – Jim Aho Jan 26 '18 at 12:05
  • @JimAho you need to `Dispose()` the `Logger`, if you configure Serilog without setting the static `Log.Logger`. – Nicholas Blumhardt Jan 26 '18 at 21:09
  • 1
    Still waiting on a proper 'Flush-no-Close' or 'Disable-Cache-until-Close' mechanic.. useful for shutdown scenarios where threads are not yet fully terminated and whose lifetime cannot be strictly guaranteed. While unfortunate, it would be nice to account for such cases. – user2864740 Mar 13 '19 at 18:13
  • Although unnecessary, would it be ok if it's called`Log.CloseAndFlush()` twice? – liang Jun 27 '19 at 05:04
0

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: https://github.com/serilog/serilog/issues/1111

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.

Nigrimmist
  • 10,289
  • 4
  • 52
  • 53