-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
68 lines (52 loc) · 1.94 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
using Windows.Networking.Sockets;
namespace TableGame
{
enum LogLevel
{
Info,
Debug,
Warning,
Error,
Fatal
}
public class Logger
{
private static readonly Logger instance = new Logger();
public ObservableCollection<string> Logs { get; private set; }
/// <summary>
/// Поле чисто для ИНФО сообщений - для отображения в интерфейс
/// </summary>
public ObservableCollection<string> LogsOnlyInfo { get; private set; }
public Logger()
{
Logs = new ObservableCollection<string>();
LogsOnlyInfo = new ObservableCollection<string>();
/* for (int i = 0; i < 200; i++)
Info("test");*/
}
public static Logger GetInstance()
{
return instance;
}
private void Add (string message, LogLevel level, ConsoleColor color)
{
var normalMessage = $"[{level.ToString()}] [{DateTime.Now}] {message}";
Logs.Add(normalMessage);
if(LogLevel.Info == level)
LogsOnlyInfo.Add ($"{DateTime.Now} - {message}");
Console.WriteLine(normalMessage, color);
}
public void Info(string message) => Add(message, LogLevel.Info, ConsoleColor.White);
public void Debug(string message) => Add(message, LogLevel.Debug, ConsoleColor.DarkGreen);
public void Warn(string message) => Add(message, LogLevel.Warning, ConsoleColor.DarkYellow);
public void Error(string message) => Add(message, LogLevel.Error, ConsoleColor.Red);
public void Fatal(string message) => Add(message, LogLevel.Fatal, ConsoleColor.DarkRed);
}
}