boost::log add_file_log not writing if app exits w

2019-03-06 05:16发布

问题:

I'm on Windows 7, using boost::log in v1.54 in a console application. I'm using add_file_log() with rotation, which works fine as long as my main() function exits. But if the app is terminated externally with powershell CloseMainWindow(), the log file never gets written.

I've set up a windows control handler using SetConsoleCtrlHandler(), and can trap CTRL_CLOSE_EVENT, but I don't know if there's a boost::log method I can call to force the log file to write prior to my app exiting. Does anyone have a solution for this?

Thanks,

  Rob

回答1:

Two thing you may want to look at. The following is based on my experience

1) The log file exists but is empty. In this case use auto_flush to flush the log to disk as the log messages are created. Other wise you could end up with an empty log if there is not enough log messages to be flushed when CTRL_CLOSE_EVENT occurs.

2) The log file exits but is not were you think it is. In this case make sure that the log file is created in the same directory as the target directory. The target directory collects the log files when the sinks are closed. If the keyword file_name is set to a file name only it will be created in the working directory, not the target directory. If the application exits without closing the the sinks then the file will not be moved to the target directory.

HTH,