cjbhaines / Log4Net.Async

Asynchronous Log4Net appenders and forwarder
http://www.nuget.org/packages/Log4Net.Async/
MIT License
121 stars 37 forks source link

AsyncForwardingAppender causing WPF application to hang on application exit #7

Closed renns closed 9 years ago

renns commented 9 years ago

I added the AsyncForwardingAppender to a WPF application, and when I exit the application, it stays running. I tracked the cause to the AsyncForwardingAppender. I tried ParallelForwardingAppender and that had no problem.

My setup: In the App.xaml.cs constructor, I have XmlConfigurator.Configure();. In my App.config, my root logger has a reference to the AsyncForwardingAppender and the AsyncForwardingAppender has references to my other appenders.

I was able to get it to exit if I put "log4net.LogManager.Shutdown();" in the Application_Exit method. However, I don't think that should be necessary.

I also have the same logging being used in windows services and they have no problem stopping.

rcollette commented 9 years ago

I wrote the ParallelForwardingAppender. It catches thread abort exceptions to shutdown, but that wouldn't be a "graceful" shutdown.

The documentation for LogManager,Shutdown specifically mentions the case of nested appenders, which is what we have here. If it works, I would use it.

That said, I'd like to contribute a modification to the AsyncForwarding appender that changes it from sleeping to using a wait. At such time that I get to look at that, I will also take a look at thread abort handling.

It took me from somewhere around Thanksgiving until now to address an issue with HttpContext being null. That will provide some guide as to how often I have an opportunity to work on side projects.

In the mean time, it would be a help if you had a shell project you could post. I'm not a XAML developer.

cjbhaines commented 9 years ago

If you are using log4net it is good practice to call LogManager.Shutdown on your application exit. In this scenario it will clean up all your logging threads. Because log4net is an extensible library you have no control over what resources it is managing so you should always stop it gracefully.