diff Logging/FileLogger.cs @ 0:961030992bd2

Initial commit of IBBoard libraries
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 11:13:48 +0000
parents
children 0352fa33ee8f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Logging/FileLogger.cs	Fri Dec 19 11:13:48 2008 +0000
@@ -0,0 +1,54 @@
+using System;
+using System.IO;
+
+namespace IBBoard.Logging
+{	
+	public abstract class FileLogger : Logger
+	{			
+		public FileLogger() : this(CreateDefaultLogFileStream())
+		{
+		}
+		
+		public FileLogger(string path) : this(CreateLogFileStream(path))
+		{
+		}
+		
+		public FileLogger(FileInfo file) : this(CreateLogFileStream(file))
+		{
+		}
+		
+		public FileLogger(FileStream stream) : base(stream)
+		{
+		}
+		
+		public static string MakeDefaultLogFilePath()
+		{
+			return MakeLogFilePath(Constants.UserDataPath);
+		}
+		
+		public static string MakeLogFilePath(string path)
+		{
+			return path.TrimEnd(IBBoard.Constants.DirectoryChar) + IBBoard.Constants.DirectoryChar + "logs" + IBBoard.Constants.DirectoryChar + String.Format("{0:yyyy-MM-dd-HHmmss}", DateTime.Now)+".log";
+		}
+		
+		public static FileStream CreateDefaultLogFileStream()
+		{
+			return CreateLogFileStream(MakeDefaultLogFilePath());
+		}
+		
+		public static FileStream CreateLogFileStream(string path)
+		{
+			return CreateLogFileStream(new FileInfo(path));
+		}
+		
+		public static FileStream CreateLogFileStream(FileInfo file)
+		{
+			if (!file.Directory.Exists)
+			{
+				file.Directory.Create();
+			}
+			
+			return new FileStream(file.FullName, FileMode.OpenOrCreate, FileAccess.Write);
+		}
+	}
+}