c# write logger using filename “log.txt”
- User
c# write logger using filename “log.txt”
- Assistant
Here's an example of how you can write a logger in C# using the filename "log.txt":
using System; using System.IO; public static class Logger { private static readonly string LogFileName = "log.txt"; public static void Log(string message, bool appendTimestamp = true) { using (StreamWriter writer = new StreamWriter(LogFileName, true)) { if (appendTimestamp) { string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); writer.WriteLine($"[{timestamp}] {message}"); } else { writer.WriteLine(message); } } } }In this example, the
Loggerclass provides a static methodLogthat takes in a message to be logged and an optional parameterappendTimestampthat, when set totrue, appends a timestamp to the log message. The log is written to a file named "log.txt".To use the logger, you can call the
Logmethod like this:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.