Utils/FileLogger.cs

42 lines
1.3 KiB
C#
Raw Normal View History

2015-11-16 00:38:57 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace BlubbFish.Utils {
public class FileLogger {
private static Dictionary<string, FileLogger> instances = new Dictionary<string, FileLogger>();
private StreamWriter file;
private FileLogger(string filename, bool append) {
if(!File.Exists(filename)) {
string folder = Path.GetDirectoryName(Path.GetFullPath(filename));
if(!Directory.Exists(folder)) {
Directory.CreateDirectory(folder);
2015-11-16 00:38:57 +01:00
}
}
this.file = new StreamWriter(filename, append, Encoding.UTF8);
this.file.AutoFlush = true;
}
public static FileLogger getInstance(string filename, bool append) {
if(!instances.Keys.Contains(filename)) {
instances.Add(filename, new FileLogger(filename, append));
}
return instances[filename];
}
2015-11-16 00:38:57 +01:00
public void setArray(string[] text) {
this.file.Write(String.Join(file.NewLine, text) + file.NewLine);
this.file.Flush();
}
2015-11-16 00:38:57 +01:00
public void setLine(string text) {
this.file.WriteLine(text);
this.file.Flush();
}
public void setLine(string text, DateTime d) {
this.setLine(d.ToString("[yyyy-MM-dd HH:mm:ss.ffff] " + text));
2015-11-16 00:38:57 +01:00
}
}
2015-11-16 00:38:57 +01:00
}