我编写了以下代码:
public static class PLog
{
private static FileStream logFileStream;
private static string logFilePath;
public static void Initialize()
{
logFilePath = PSettings.IO.dirLogs + "log_" + DateTime.Now.ToString("ddMMyy_HHmmss") + ".txt";
if (!Directory.Exists(PSettings.IO.dirLogs)) Directory.CreateDirectory(PSettings.IO.dirLogs);
logFileStream = new FileStream(logFilePath, FileMode.Create);
}
public static void WriteLine(string text)
{
byte[] textArray = Encoding.ASCII.GetBytes("[" + DateTime.Now.ToString("dd.MM.yyyy - HH:mm:ss") + "] " + text + "\n");
logFileStream.Write(textArray, 0, textArray.Length);
}
public static void Close()
{
logFileStream.Close();
}
}
但是当我这样称呼它时,它不会向文件写入任何内容:
PLog.Initialize();
PLog.WriteLine("test");
PLog.Close();
该文件已创建,但它的大小为 0 字节。