-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.cs
51 lines (44 loc) · 1.63 KB
/
Logger.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
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
namespace ElectricShimmer
{
public enum LogLevel
{
ALL,
INFO,
ERROR,
EXCEPTION,
OFF
}
class Log
{
public static LogLevel Level = LogLevel.ALL;
private static Mutex mutex = new Mutex(false, "Logger");
public static void Write(string message, LogLevel logLevel, [CallerMemberName] string callerMemberName = "")
{
try
{
if (!String.IsNullOrEmpty(message) && logLevel >= Level)
{
mutex.WaitOne(1000);
DateTime DTNow = DateTime.Now;
string Date = DTNow.ToString("yyyy-MM-dd");
bool FileExists = false;
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Logger\"))
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"Logger\");
FileExists = File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Logger\ElectricShimmer.log");
StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"Logger\ElectricShimmer.log", FileExists);
sw.WriteLine(DTNow.ToString("yyyy-MM-ddTHH:mm:ss") + "\t" + logLevel.ToString() + "\t" + callerMemberName + "\t" + message);
sw.Close();
mutex.ReleaseMutex();
}
}
catch (Exception exc)
{
Write(exc.Message, LogLevel.EXCEPTION);
}
}
}
}