我有一个日志类,如下所示。
public class Log
{
public StreamWriter logFile
{ get; set; }
public string logFilePath = Environment.GetEnvironmentVariable("Temp")+"\\testlog.txt";
public string LogFilePath
{
get
{
return logFilePath;
}
set
{
value = logFilePath;
}
}
public void WriteLog(string logMessage)
{
try
{
if (!File.Exists(logFilePath))
{
logFile = new StreamWriter(logFilePath);
}
else
{
logFile = File.AppendText(logFilePath);
}
logFile.WriteLine(DateTime.Now);
logFile.WriteLine(logMessage.ToString());
logFile.WriteLine();
logFile.Flush();
}
catch
{
}
}
}
我使用下面给出的对象在类中调用了上述WriteLog
函数。Log
Log lg = new Log();
lg.WriteLog("message1");
lg.WriteLog("message2");
lg.WriteLog("message3");
lg.WriteLog("message4");
问题是只有“message1”被添加到日志中。不写入所有其他消息。
我该如何解决这个问题?