Reputation: 7030
public static class LogWriter
{
private static ReaderWriterLockSlim writeLock = new ReaderWriterLockSlim();
public static void WriteExceptionLog(string content)
{
#if DEBUG
MessageBox.Show(content);
#endif
WriteLog(content, Constant.EXCEPTION_LOG_PATH);
}
public static void WriteLog(string content, string path)
{
try
{
writeLock.EnterWriteLock();
string directory = Path.GetDirectoryName(path);
if (!Directory.Exists(Path.GetDirectoryName(directory)))
Directory.CreateDirectory(directory);
using (StreamWriter writeFile = new StreamWriter(path, true))
{
content = DateTime.Now + " : " + content;
writeFile.WriteLine(content);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
writeLock.ExitWriteLock();
}
}
}
I have a class that writes logs. Because I am writing to a log asynchronously, I need to put a lock and release it when write is done but this seems to be somewhat of a clunky solution and maybe even bad for performance.
What is a better way to handle this?
Upvotes: 2
Views: 1727
Reputation: 14853
For performance reason and also to avoid a very different behavior between log-ON and log-OFF, I suggest to run one buffered log file per thread.
The counterparts are:
To go one step more to real-time, you have to log in memory, and to develop a dedicated interface to extract log on request but this kind of log is generally reserved to hard real-time embedded application.
Other solution for safe logging with low CPU consumption (low level C programming):
If the observed process crash, no log record will be lost since shared memory segment is attached to log manager process.
Upvotes: 3
Reputation: 8141
Opening, writing to, and then closing the file on each log request is both redundant and inefficient.
On your log class, use a buffer, and write that buffer's contents to the file, either every X requests, shutdown, or every Y minutes.
Upvotes: 0