Logging wrapper function review
Very new to .NET programming.I am writing simple Logging wrapper function in class that can be called from many apps. This will create new log file everyday and log exceptions etc to that Log file.
I am using this code below: Please review especially this line:
using (StreamWriter sw = new StreamWriter(File.Open(file, System.IO.FileMode.Append)))
is it ok to open the file to append when writing to it everytime or will it create overhead?
public class LogUtlity
{
public static string logFile;
public static string logFilePath;
public static string file;
public LogUtlity()
{
logFilePath = ConfigurationManager.AppSettings["filePath"];
logFile = "log" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
//Create new file everyday
if (!(File.Exists(logFilePath)))
{
file = Path.Combine(logFilePath, logFile);
}
}
public void WriteLog(string logMessage)
{
using (StreamWriter sw = new StreamWriter(File.Open(file, System.IO.FileMode.Append)))
{
sw.WriteLine(String.Format("{0:G}: {1}.", DateTime.Now, logMessage));
}
}