Junior — Middle
Do you have experience logging to files for system or application purposes?
sobes.tech AI
Answer from AI
Yes, I have experience logging to files for system and application purposes. In C#, this is often done using the StreamWriter class or specialized libraries such as NLog or log4net.
A simple example of logging to a file using StreamWriter:
using System;
using System.IO;
public class Logger
{
private readonly string logFilePath;
public Logger(string path)
{
logFilePath = path;
}
public void Log(string message)
{
using (var writer = new StreamWriter(logFilePath, append: true))
{
writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}");
}
}
}
This approach allows saving logs with timestamps, which is useful for debugging and monitoring application performance.