C# 获取正确的日期时间

C# 获取正确的日期时间,c#,datetime,datetime-format,C#,Datetime,Datetime Format,我有一个文件可以在文本文件中创建日志条目,但是日期时间不起作用,我尝试设置为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

我有一个文件可以在文本文件中创建日志条目,但是日期时间不起作用,我尝试设置为
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]


无论服务器运行多长时间,以及日志中添加了多少行,上述日期时间都不会更改。

您正在创建一个新的
日期时间,该日期时间将初始化为,然后继续重复使用

无论何时调用
Info
您都希望通过或获取当前
DateTime

public static void Info(string text)
{
    text = string.Format("[{0:d/M/yyyy HH:mm:ss}] {1}", DateTime.Now, text);
    plainWrite(text); 
}