I'm writing a tool that's going to be check the health of workstations across a network, and will fix according to the issues it finds. I want to create a log file as the app is running through its tasks / checks on each machine. I just want to get this working on a single machine for now, but in time it will be scanning 100+ machines in one go (Threaded out).
What is the best way to create a log file?
I was thinking of using a List<string>
to build up the log file in memory and then output it to a file once it had finished.
I'm just thinking there may be a better way of doing this?
I found the SimpleLogger from heiswayi on GitHub good.
We did a lot of research into logging, and decided that NLog was the best one to use.
See http://nlog-project.org/
Also see log4net vs. Nlog and http://www.dotnetlogging.com/comparison/
I would not use third party libraries, I would log to an xml file.
This is a code sample that do logging to a xml file from different threads:
You can use http://logging.apache.org/ library and use a database appender to collect all your log info together.
You can also take a look at the built-in .NET tracing facilities too. There's a set of trace listeners that allow you to output to a log file, but you can configure it to log into the Event viewer, or to a database (or all of them simultaneously).
http://www.codeguru.com/csharp/.net/net_debugging/tracing/article.php/c5919/NET-Tracing-Tutorial.htm
I would recommend log4net.
You would need multiple log files. So multiple file appenders. Plus you can create the file appenders dynamically.
Sample Code:
Sources/Good links:
Log4Net: Programmatically specify multiple loggers (with multiple file appenders)
Adding appenders programmatically
How to configure log4net programmatically from scratch (no config)
Plus the log4net also allows to write into event log as well. Everything is configuration based, and the configuration can be loaded dynamically from xml at runtime as well.
Edit 2:
One way to switch log files on the fly: Log4Net configuration file supports environment variables:
and in the log4net config: