我有一个文件可以在文本文件中创建日志条目,但是日期时间不起作用,我尝试设置为Utcnow
但没有运气
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ParticleFramework
{
static class Log
{
private static FileStream file;
public static string dateTime = String.Format("{0:d/M/yyyy HH:mm:ss}", new DateTime());
public static string logFile;
public static Boolean Initialize(string f)
{
logFile = f;
if (logFile != "")
{
try
{
file = new FileStream(logFile, FileMode.Append);
plainWrite("");
plainWrite("");
Info("Particle Server logging started...");
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
public static void Info(string text)
{
text = "[" + dateTime + "] " + text;
plainWrite(text);
}
public static void Error(string text)
{
text = "[ERROR]" + text;
plainWrite(text);
}
private static void plainWrite(string text)
{
try
{
StreamWriter sw = new StreamWriter(file);
sw.WriteLine(text);
sw.Close();
file = new FileStream(logFile, FileMode.Append);
}
catch
{
}
}
}
}
[1/1/0001 00:00:00]
无论服务器运行多长时间,以及向日志添加了多少行,上述日期时间都不会改变。
答案 0 :(得分:3)
您正在创建一个新的DateTime
,它将初始化为DateTime.MinValue
,然后继续重复使用。
每当您致电Info
时,您希望通过DateTime.Now
或DateTime.UtcNow
获取当前DateTime
:
public static void Info(string text)
{
text = string.Format("[{0:d/M/yyyy HH:mm:ss}] {1}", DateTime.Now, text);
plainWrite(text);
}