From this question and some help I created a thread safe logging class on which the name of the log file is same as the current date (short date) when the log entry was added. e.g. If I create ten entries today say they will all get written to log file named "07.10.2015.txt". I want to use this class from DLL. I am curious on your opinion if this is reasonable and thread safe class?
public static class Logger
{
static readonly object _syncObject = new object();
public static void Write(string logMessage)
{
try
{
var todaysLogFilePath = Path.Combine(ConfigManager.GetAppSetting("logPath"),
("Log" + DateTime.Now.ToString("yyyy-MMMM-dd") + ".txt"));
Log(logMessage, todaysLogFilePath);
}
catch (IOException e)
{
//log somewhere else if required
}
}
static void Log(string logMessage, string logFilePath)
{
lock (_syncObject)
{
File.AppendAllText(logFilePath,
string.Format("{0} {1} {2}",
DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString(), logMessage));
}
}
}
PS. I don't want third party classes because they have some requirements sometimes if used from DLL which is my requirement, and in some cases I don't know directly how to enable in those classes ability to name the log entries same as the current date when log was added.