Skip to main content

A simple helper class to write informational messages to the system console and file system (log file).

using System;
using System.IO;

public static class Log
{
	static string _logFile;
	static object _lock = new object ();

	public static void Init(string logFile)
	{
		_logFile = logFile;
	}

	public static void Error(string message)
	{
		if (_logFile != null) {
			lock (_lock)
				File.AppendAllText(_logFile, "[ERROR] " + message + Environment.NewLine);
		}
		Console.WriteLine("[ERROR] " + message);
	}

	public static void Info(string message)
	{
		if (_logFile != null) {
			lock (_lock)
				File.AppendAllText(_logFile, "[INFO] " + message + Environment.NewLine);
		}
		Console.WriteLine(message);
	}

	public static void Debug(string message)
	{
		if (_logFile != null)
		{
			lock (_lock)
				File.AppendAllText(_logFile, "[DEBUG] " + message + Environment.NewLine);
		}
	}
}