-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (46 loc) · 1.85 KB
/
Program.cs
File metadata and controls
56 lines (46 loc) · 1.85 KB
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
using System;
using System.Collections.Generic;
namespace Singleton
{
internal class Program
{
static void Main(string[] args)
{
GameLogger.Instance.LogInfo("Игрок загрузился в локации \"Лес\"");
GameLogger.Instance.LogInfo("Игрок получил 10 урона");
GameLogger.Instance.LogWarning("У игрока осталось 5 здоровья!");
GameLogger.Instance.LogInfo("Подобран предмет \"Целебное зелье\"");
GameLogger.Instance.LogError("Не удалось сохранить игру: файл занят");
Console.WriteLine("\n=== ВСЕ ЛОГИ ===");
GameLogger.Instance.PrintAllLogs();
}
}
public class GameLogger
{
public static GameLogger Instance { get; } = new GameLogger();
private List<string> _logs = new List<string>();
private GameLogger() { }
private void Log(string message)
{
string time = DateTime.Now.ToString("HH:mm:ss");
string formattedMessage = $"[{time}] {message}";
Console.WriteLine(formattedMessage);
_logs.Add(formattedMessage);
}
public void LogInfo(string message) => Log($"[INFO] {message}");
public void LogWarning(string message) => Log($"[WARNING] {message}");
public void LogError(string message) => Log($"[ERROR] {message}");
public void PrintAllLogs()
{
if (_logs.Count == 0)
{
Console.WriteLine("Лог пуст");
return;
}
for (int i = 0; i < _logs.Count; i++)
{
Console.WriteLine($"{i + 1}. {_logs[i]}");
}
}
}
}