-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
50 lines (45 loc) · 1.37 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace potter
{
internal static class Logger
{
private static string logFilePath;
private static object logMutex = new object();
static Logger()
{
logFilePath = Path.Combine(Path.GetTempPath(), "potter.log");
}
internal static void Append(string entry)
{
DateTime timestamp = DateTime.Now;
Monitor.Enter(logMutex);
try
{
using (var file = File.Open(logFilePath, FileMode.Append))
using (var stream = new StreamWriter(file))
{
stream.Write(timestamp.ToShortDateString());
stream.Write(" ");
stream.Write(timestamp.ToShortTimeString());
stream.Write(" ");
stream.Write(Process.GetCurrentProcess().Id);
stream.Write(" ");
stream.Write(Thread.CurrentThread.ManagedThreadId);
stream.Write(": ");
stream.WriteLine(entry);
}
}
finally
{
Monitor.Exit(logMutex);
}
}
}
}