Currently my application writes debug messages and exceptions to a log file in a thread-safe manner. It’s very simple and I do not need any framework (i.e. log4net) to do this.
This is how I am doing it at present:
private static void Write(string message)
{
Task.Factory.StartNew(() =>
{
try
{
lock (locker)
{
File.AppendAllText(DebugFilePath, message);
InvokeLogWrittenEvent(message);
}
}
catch
{
}
});
}
Is this ok or do you think I should have a dedicated thread which has a BlockingCollection that processes items added into the collection? Both will give me the same output, but I’m not sure if there are any performance implications.
The problem with performing logging within your application process is that it will effect performance and give you duff results in the case of it crashing (it will also hold up application closure if its writing to a file).
Running a separate primary thread that you pass logging message to is by far a more efficient manner, although requires more coding as it will have far less impact on your applications performance.