System.ObjectDisposedException Message: Cannot access a closed file

Feb 2, 2009 at 5:43 PM

Hi I'm getting the following exception when writing to the log:

Exception Type: System.ObjectDisposedException
Message: Cannot access a closed file.
Data: System.Collections.ListDictionaryInternal
TargetSite: Void FileNotOpen()
HelpLink: NULL
Source: mscorlib

StackTrace Information Details:
   at System.IO.__Error.FileNotOpen()
   at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Flush()
   at System.Diagnostics.TextWriterTraceListener.Flush()
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogSource.TraceData(TraceEventType eventType, Int32 id, LogEntry logEntry, TraceListenerFilter traceListenerFilter)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.ProcessLog(LogEntry log)
Priority: -1
EventId: 6352
Severity: Error

Has anyone had a similar issue with this?

Feb 3, 2009 at 12:32 AM

can you please provide the steps to reproduce your scenario?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Feb 3, 2009 at 1:38 AM
I have a web application project, "core" code project and two windows services projects. 4 projects. The web application and two windows services reference the core code. The core code has a static LogHelper class that is  using the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling and  Microsoft.Practices.EnterpriseLibrary.Logging;

Windows service 1 checks a file folder every hour and writes a message to the log accordingly (files found versus no files to process). All it does is count the number of files in a folder (Directory.GetFiles().Length)
Windows service 2 checks for certain data every five minutes in a database and writes a message to the log.
Windows service 1 has its log in its own folder c:\windows service 1\Log\trace.log.
Windows service 2 has its log in its own folder c:\windows service 2\Log\trace.log.
There are errors in the event viewer that occurs when windows service 1 writes the message to the log. The exception I see is what is posted above.
There are no errors in the event viewer that for windows service 2.

LogHelper class:


static void WriteLog(String message)




LogEntry logEntry = new LogEntry();


logEntry.EventId = 100;

logEntry.Priority = 2;

logEntry.Message = message;





"UI Events");









Is it possible that even though the two windows services have their own references to the core code and that the trace.logs are in separate folders that there is a "collision" so to speak?

Feb 3, 2009 at 5:13 AM

do you have both separate app.config for each windows service? and also have you tried removing the Logger.Writer.Dispose()? and see if the exception still persist?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Feb 3, 2009 at 2:23 PM
Yes I do have separate app.config's for each windows service? I have not tried and remove the logger.writer.dispose();
Feb 4, 2009 at 3:33 AM
Edited Feb 4, 2009 at 3:36 AM
I suggest you try out Valiant's suggestion.  Are you getting this error intermittently? Or you can always repro the error with the same exact steps?  

Check out also this thread..,

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
Feb 4, 2009 at 1:16 PM
It's happening every hour on the hour for the windows service 1. I never see any errors from windows service 2 and like I said its writing something at minimum every five minutes. Windows service 1 is writing something every hour and that's exactly when I see it the errors in the event viewer.
Feb 5, 2009 at 4:12 AM
Have you tried my suggestion? What happens when you remove the Logger.Writer.Dispose()? I cant seem to repro your error. Would you mind sending me your solution so I can investigate further?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Feb 5, 2009 at 9:19 PM
I made the change to the core code (removed the Logger.Writer.Dispose()), built it, rebuilt the two windows services and implemented them onto the server last night. I've been monitoring the event viewer all day and have not seen anymore errors. so, that seemed to work. How I'm not sure seeing as they are separate services, separate configs. I wouldn't have thought that there would be a conflict.