C# Singleton Logging Class

This is taken care of for you automatically if you use NLog - you define all of your loggers in a .config file and then you access all of them via the static LogManager class, which is a Singleton.

Here's an example which illustrates the thread-safe nature of NLog:

https://github.com/nlog/nlog/wiki/Tutorial#Adding_NLog_to_an_application


There's a method TextWriter.Synchronized which produces a thread-safe version of TextWriter. Try that.


a) Do not include "Log" in method names. It obvious that a logger logs. .Warning, .Error, etc are better method names since they describe which level the log entry has.

b) Create a background thread that writes to the log.

c) Enqueue entries from the logging methods and signal the worker thread.

d) Use (I don't know if I remember the method names correctly)

var methodInfo = new StackFrame(1).GetMethod();
var classAndMethod = methodInfo.DeclaringType.Name + "." + methodInfo.Name;

to get the calling method.

Doing that will give you only one thread that access the file.