forked from RazTools/Studio
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathILogger.cs
76 lines (68 loc) · 1.89 KB
/
ILogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace AssetStudio
{
[Flags]
public enum LoggerEvent
{
None = 0,
Verbose = 1,
Debug = 2,
Info = 4,
Warning = 8,
Error = 16,
All = Verbose | Debug | Info | Warning | Error,
}
public interface ILogger
{
void Log(LoggerEvent loggerEvent, string message);
}
public sealed class DummyLogger : ILogger
{
public void Log(LoggerEvent loggerEvent, string message) { }
}
public sealed class ConsoleLogger : ILogger
{
public void Log(LoggerEvent loggerEvent, string message)
{
Console.WriteLine("[{0}] {1}", loggerEvent, message);
}
}
public sealed class FileLogger : ILogger
{
private const string LogFileName = "log.txt";
private const string PrevLogFileName = "log_prev.txt";
private readonly object LockWriter = new object();
private StreamWriter Writer;
public string logPath;
public string prevLogPath;
public FileLogger()
{
logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LogFileName);
prevLogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PrevLogFileName);
if (File.Exists(logPath))
{
File.Move(logPath, prevLogPath, true);
}
Writer = new StreamWriter(logPath, true) { AutoFlush = true };
}
~FileLogger()
{
Dispose();
}
public void Log(LoggerEvent loggerEvent, string message)
{
lock (LockWriter)
{
Writer.WriteLine($"[{DateTime.Now}][{loggerEvent}] {message}");
}
}
public void Dispose()
{
Writer?.Dispose();
}
}
}