時,這是一個記錄類構造函數拋出異常:C#FileLogTraceListener關閉
public QFXLogger(
int maxFileSize,
TraceLevel logLevel)
{
this.maxFileSize = maxFileSize;
logSwitch.Level = logLevel;
//Configure log listener
traceListener = new FileLogTraceListener();
traceListener.DiskSpaceExhaustedBehavior = DiskSpaceExhaustedOption.DiscardMessages;
traceListener.CustomLocation = @".\Log";
traceListener.BaseFileName = "QFXLog";
traceListener.AutoFlush = true;
//Remove all other listeners
Trace.Listeners.Clear();
//Add QFX listener
Trace.Listeners.Add(traceListener);
//Write header
WriteSessionHeader();
}
這是destrcutor:
~QFXLogger()
{
WriteSessionFooter();
traceListener.Close();
}
我只想之前寫一個頁腳底層流記錄器獲取GC。 沒有析構函數一切都很好,但它我得到以下幾點:
Unhandled Exception: System.ObjectDisposedException: Cannot access a closed file
.
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.Flush(Boolean flushToDisk)
at System.IO.FileStream.Flush()
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Flush()
at Microsoft.VisualBasic.Logging.FileLogTraceListener.ReferencedStream.CloseS
tream()
at Microsoft.VisualBasic.Logging.FileLogTraceListener.CloseCurrentStream()
at Microsoft.VisualBasic.Logging.FileLogTraceListener.Write(String message)
at System.Diagnostics.TraceInternal.Write(String message)
at System.Diagnostics.Trace.Write(String message)
at QFXShell.QFXLogger.WriteSessionFooter()
at QFXShell.QFXLogger.Finalize()
在我看來,底層流已經關閉。
我該如何抑制這種關閉(底層流)或者這是另一個問題?
@Ramhound WriteSessionFooter除此之外不做任何事情:Trace.Write(footerString);問題是我想在應用程序關閉之前編寫結束語句。 – Juergen
當我在我的類的IDisposable接口實現中調用Stream.Dispose()時發生了此ObjectDisposedException,但在移除對Stream.Flush()的調用函數調用後停止了此操作。 – Roger
哎呀,說得太快。通過確保我的Dispose(bool isFreeingManagedResources)函數被虛擬保護,解決了這個問題,正如Kelly Leahy在他的網站上解釋的那樣。以前,我把它當作私人的。 – Roger