-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileLogger.cs
58 lines (51 loc) · 1.28 KB
/
FileLogger.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
using Meadow;
using Meadow.Logging;
using System;
using System.IO;
using System.Linq;
namespace Logging;
public class FileLogger : ILogProvider
{
private string LogFilePath { get; }
public FileLogger()
{
LogFilePath = Path.Combine(MeadowOS.FileSystem.DocumentsDirectory, "meadow.log");
if (!File.Exists(LogFilePath))
{
File.Create(LogFilePath).Close();
}
}
public void Log(LogLevel level, string message, string messageGroup)
{
switch (level)
{
case LogLevel.Warning:
case LogLevel.Error:
LogToFile(message);
break;
}
}
private void LogToFile(string message)
{
if (message.EndsWith(Environment.NewLine))
{
File.AppendAllText(LogFilePath, message);
}
else
{
File.AppendAllText(LogFilePath, message + Environment.NewLine);
}
}
public string[] GetLogContents()
{
if (!File.Exists(LogFilePath))
{
return new string[0];
}
return File.ReadLines(LogFilePath).ToArray();
}
public void TruncateLog()
{
File.Create(LogFilePath).Close();
}
}