Disposing LogWriter in WebApplications

Topics: Logging Application Block
May 13, 2015 at 2:33 PM

I am creating a logging solution for a ASP.NET MVC application and I am creating the LogWriter with the following piece of code in the Application_Start of Global.asax.cs file:

Logger.SetLogWriter(new LogWriterFactory().Create());

And I'm able to log successfully within the application. The question I have is, where should I call the LogWriter.Dispose method in this scenario of a Web App based on the reasons given by RandyLevy in the answer to the following post: https://entlib.codeplex.com/discussions/581824.

Any help would be much appreciated.
May 14, 2015 at 4:52 AM
Typically, you don't need to call Dispose(). If at application startup you are bootstrapping the block and using the LogWriter singleton for the duration of the application then all resources will be released when the application shuts down.

You would manually call Dispose() on the existing LogWriter when creating a new LogWriter and setting that using SetLogWriter(). This would usually be done when performing some reconfiguration of the LogWriter Settings (first you would want to Dispose the existing LogWriter and then set the newly configured LogWriter). In the linked question the user was calling SetLogWriter on page load (not application start) so when multiple concurrent requests occur there could be strange errors.
Marked as answer by Debarshi on 5/14/2015 at 12:51 AM
May 14, 2015 at 8:51 AM
Thanks for the prompt response Randy, much appreciated.