1

I have a program which crashes unexpetedly. I have put NLog to provide me information about where it crashes but it doesn't achieve its goal for it logs up to 30" before the crash. So I have to change something.

I have seen that there should the command NFlush from here but as a matter of fact there isn't

enter image description here

I have version 4.7.10.

But if it was I might use in some strategic points.

So my questions are: how to flush? And is there instead another way to make NLog flush more frequently?

Thanks

Patrick

Patrick
  • 3,073
  • 2
  • 22
  • 60
  • 1
    If the crash is not in the log, try and install an unhandled exception handler and log from there. Also don't forget to _close_ the log. See https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception?view=net-5.0 and https://nlog-project.org/documentation/v4.3.0/html/M_NLog_LogManager_Shutdown.htm – Fildor May 26 '21 at 07:46
  • Thanks for helping, I have edited my question. As for your answer I already have it but it doesn't seems working. Anyhow in that I would like to flush and shutdown but as edited above it seems I haven't got those commands. – Patrick May 26 '21 at 08:08
  • 1
    Is the screenshot taken from an `ILogger` instance or `LogManager` ? – Fildor May 26 '21 at 08:12
  • 1
    Ah thanks a million, so I have to do LogManager.Flush() and LogManager.Shutdown(). Also from https://stackoverflow.com/questions/59773319/nlog-does-not-flush-all-log-entries-upon-process-exit I have seen how to improve its writing – Patrick May 26 '21 at 08:15
  • If you do not have a better strategy you might want to put that as answer to my question – Patrick May 26 '21 at 08:23
  • I would but the flush after every write to make sure all the logs get save before the crash. – jdweng May 26 '21 at 08:29
  • Also, you may want to go through your code and make sure there no empty `catch` blocks or `async void` methods ... – Fildor May 26 '21 at 08:33
  • @jdweng not sure that this could be correct. It might affect performances. What about the LogManager.AutoShutdown feature?! – Patrick May 26 '21 at 08:38
  • 2
    The application could crash before the AutoShutdown will occur. Right now you are trying to solve an issue, and you shouldn't worry about performance until after you solve the critical problem. Later you can decide if you want to make the flush a permanent feature. – jdweng May 26 '21 at 09:23

1 Answers1

2

The screenshot with intellisense-suggestions comes from the NLog Logger-object. The NLog.LogManager.Flush() is a static-method.

NLog automatically attempts to flush when being notified that application is about to exit. This is done by hooking into both AppDomain.ProcessExit + AppDomain.DomainUnload

This automatic flush/shutdown usually works okay on .NetFramework running on Windows. But when running on other platforms using NetCore, then the AppDomain-events can be fired very early before the application has even started to shutdown. Thus causing NLog to stop logging while application is still in progress of shutting down. Microsoft seems to be working on improving this behavior, but not sure if Net60 will include the fixes. NLog.LogManager.AutoShutdown = false tells NLog that the user will explictly handle flush/shutdown.

The simple solution is just to use NLog FileTarget or NLog ConsoleTarget (they flush automatically), and without using <targets async="true">. And if running on exotic platforms, where Microsoft have failed to implement correct signal of process-exit, then configure NLog.LogManager.AutoShutdown = false.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70