1

I have a file to create log entries into a text file, but the datetime will not work, I have tried setting to Utcnow but to no luck

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]

No matter how long the server runs, and how many lines are added to the log, the above datetime does not change.

1 Answer 1

3

You are creating a new DateTime that will be initialized to DateTime.MinValue and then just keep reusing that.

Whenever you call Info you want to get the current DateTime with DateTime.Now or DateTime.UtcNow:

public static void Info(string text) { text = string.Format("[{0:d/M/yyyy HH:mm:ss}] {1}", DateTime.Now, text); plainWrite(text); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.