如果我在整个应用程序生命周期中需要 StreamWriter,应该在哪里处理它?我要把它放在析构函数中,这行得通吗?我必须处理以刷新数据,并且我不想使用AutoFlush
功能,因为来自 msdn:"You can get better performance by setting AutoFlush to false, assuming that you always call Close (or at least Flush) when you're done writing with a StreamWriter."
那么我应该Dispose
像下面的代码那样使用析构函数吗?
class Log
{
private static StreamWriter swLog = new StreamWriter("logMAIN.txt");
static ~Log()
{
swLog.Dispose();
}
public static void Push(LogItemType type, string message)
{
swLog.WriteLine(type + " " + DateTime.Now.TimeOfDay + " " + message);
}
}
upd而不是Dispose
我打算打电话Close
,但在这种情况下它并不重要,因为它们看起来完全一样。