获取正确的日期时间
Getting correct datetime
我有一个文件可以将日志条目创建到文本文件中,但是日期时间不起作用,我尝试设置为 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]
无论服务器运行多长时间,日志中添加了多少行,上面的日期时间都不会改变。
您正在创建一个新的 DateTime
,它将被初始化为 DateTime.MinValue
,然后继续重复使用它。
每当您调用 Info
时,您都希望通过 DateTime.Now
or 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);
}
我有一个文件可以将日志条目创建到文本文件中,但是日期时间不起作用,我尝试设置为 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]
无论服务器运行多长时间,日志中添加了多少行,上面的日期时间都不会改变。
您正在创建一个新的 DateTime
,它将被初始化为 DateTime.MinValue
,然后继续重复使用它。
每当您调用 Info
时,您都希望通过 DateTime.Now
or 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);
}