2015-11-16 00:38:57 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
2016-06-08 13:25:47 +02:00
|
|
|
|
using System.Reflection;
|
2015-11-16 00:38:57 +01:00
|
|
|
|
|
2017-03-09 23:32:42 +01:00
|
|
|
|
namespace BlubbFish.Utils
|
|
|
|
|
{
|
|
|
|
|
public class FileLogger
|
|
|
|
|
{
|
|
|
|
|
private static Dictionary<String, FileLogger> instances = new Dictionary<String, FileLogger>();
|
2016-06-08 13:25:47 +02:00
|
|
|
|
private static String logDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar;
|
2018-05-15 20:49:03 +02:00
|
|
|
|
private readonly StreamWriter file;
|
2017-03-09 23:32:42 +01:00
|
|
|
|
private FileLogger(String filename, Boolean append)
|
|
|
|
|
{
|
2016-06-08 13:25:47 +02:00
|
|
|
|
filename = logDir + filename;
|
2017-03-09 23:32:42 +01:00
|
|
|
|
if (!File.Exists(filename)) {
|
|
|
|
|
String folder = Path.GetDirectoryName(Path.GetFullPath(filename));
|
|
|
|
|
if (!Directory.Exists(folder)) {
|
2016-01-26 15:20:42 +01:00
|
|
|
|
Directory.CreateDirectory(folder);
|
2015-11-16 00:38:57 +01:00
|
|
|
|
}
|
2016-01-26 15:20:42 +01:00
|
|
|
|
}
|
2017-03-09 23:32:42 +01:00
|
|
|
|
this.file = new StreamWriter(filename, append, Encoding.UTF8) {
|
|
|
|
|
AutoFlush = true
|
|
|
|
|
};
|
2016-01-26 15:20:42 +01:00
|
|
|
|
}
|
2017-03-09 23:32:42 +01:00
|
|
|
|
public static FileLogger GetInstance(String filename, Boolean append)
|
|
|
|
|
{
|
|
|
|
|
if (!instances.Keys.Contains(filename)) {
|
2016-01-26 15:20:42 +01:00
|
|
|
|
instances.Add(filename, new FileLogger(filename, append));
|
|
|
|
|
}
|
|
|
|
|
return instances[filename];
|
|
|
|
|
}
|
2015-11-16 00:38:57 +01:00
|
|
|
|
|
2017-03-09 23:32:42 +01:00
|
|
|
|
public static void SetLogDir(String v)
|
|
|
|
|
{
|
2016-06-08 13:25:47 +02:00
|
|
|
|
v = v.Replace("..", "");
|
|
|
|
|
v = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + v;
|
2017-03-09 23:32:42 +01:00
|
|
|
|
if (Directory.Exists(v)) {
|
2016-06-08 13:25:47 +02:00
|
|
|
|
logDir = v;
|
|
|
|
|
} else {
|
|
|
|
|
Directory.CreateDirectory(v);
|
|
|
|
|
logDir = v;
|
|
|
|
|
}
|
2017-03-09 23:32:42 +01:00
|
|
|
|
if (logDir.Substring(logDir.Length - 1) != Path.DirectorySeparatorChar.ToString()) {
|
2016-06-08 13:25:47 +02:00
|
|
|
|
logDir = logDir + Path.DirectorySeparatorChar;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 23:32:42 +01:00
|
|
|
|
public void SetArray(String[] text)
|
|
|
|
|
{
|
|
|
|
|
this.file.Write(String.Join(this.file.NewLine, text) + this.file.NewLine);
|
2016-01-26 15:20:42 +01:00
|
|
|
|
this.file.Flush();
|
|
|
|
|
}
|
2015-11-16 00:38:57 +01:00
|
|
|
|
|
2017-03-09 23:32:42 +01:00
|
|
|
|
public void SetLine(String text)
|
|
|
|
|
{
|
2016-01-26 15:20:42 +01:00
|
|
|
|
this.file.WriteLine(text);
|
|
|
|
|
this.file.Flush();
|
|
|
|
|
}
|
2017-03-09 23:32:42 +01:00
|
|
|
|
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
|
|
|
|
}
|
2016-01-26 15:20:42 +01:00
|
|
|
|
}
|
2015-11-16 00:38:57 +01:00
|
|
|
|
}
|