简体   繁体   中英

EnterpriseLibrary 6 error: Cannot access a disposed object (Object name: 'Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter')

  • I have an ASP.NET MVC 5 application
  • The app uses Logging Application Block and Exception Application Block
  • In the production environment I found the following error message happening multiple times:

Cannot access a disposed object. Object name: 'Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter'. at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.ExecuteReadOperation(Action readOperation) at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.Write(LogEntry log) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler.WriteToLog(String logMessage, IDictionary exceptionData) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler.HandleException(Exception exception, Guid handlingInstanceId) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.ExecuteHandlerChain(Exception ex, Guid handlingInstanceID)

The error seems to happen when there is an asynchronous request which fails on the server. When the server tries to handle this error, ExceptionHandling app block fails.

The problem I have is, of course, that I don't understand the reason for this error, but what bugles me the most is the fact that I cannot replicate the error.

I have tried re-initializing the AppDomain (via web.config), recycling the App pool, restarting the IIS server just before the ajax request is sent. I have also created a LogWriterWrapper singleton to set the LogWriter instance but the behavior is the same. From time to time, the error when trying to handle the exception still occurs.

I don't know if it could be related, but it seems this error only occurs when the client is ie8/ie9. It never occurred when chrome was used for example, but I guess this might be just by chance alone...

In the end, for anyone who encounters this issue, in my case it was because in some parts of the application, there was another instantiation of the LogWriter

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

Because I had some static helpers for logging and exception handling, where I initialized logwriter and also exceptionmanager, whenever an exception occurred durring the app use, which needed to be handled by the exceptionmanager, after the second manual instantiation has already ran, the exceptionamanger was still "hooked" on an instance of logwriter which was not present any more

The exception is self-explanatory - when Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter is called it was previously disposed somewhere in the code. So you should be searching for something like Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.Dispose() or eventually using (Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter) { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM