我用c#時寫了一個類用來保存日志,大概的做法是:
[csharp]
// write logs to file
public class LogInfo
{
static public LogInfo getInstance() {
if (pLogInfo == null)
pLogInfo = new LogInfo();
return pLogInfo;
}
private LogInfo()
{
iLine = 0;
logFile = new StreamWriter(@"Gobang.log", true);
logFile.AutoFlush = true;
}
~LogInfo()
{
// why this operation can cause something that Windows regards as a problem?
logFile.Close();
}
public void WriteLog(string strInfo)
{
logFile.WriteLine(iLine + " " + strInfo);
iLine++;
}
public void WriteLogFlush(string strInfo)
{
WriteLog(strInfo);
}
private static int iLine;
private StreamWriter logFile;
static private LogInfo pLogInfo;
}
因為在析構時關閉StreamWriter的操作,每次在關閉該windows form的程序時windows都提示該程序正在關閉,並且調用“find a solution to the problem..."的處理機制。有人知道是為什麼嗎?