-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileLogger.cs
97 lines (84 loc) · 3.01 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Collections.Concurrent;
using System.Text.Json;
using Timer = System.Timers.Timer;
namespace SmileGatewayCore.Utils.Logger;
public class FileLogger
{
public static FileLogger Instance { get; set; } = new FileLogger();
private string _serviceName = "gateway-service";
public ConcurrentQueue<LogModel> _queue = new ConcurrentQueue<LogModel>();
public string _path = "";
public void Init(string path)
{
// 경로 측정 및 확인 해야함.
_path = path;
Timer timer = new Timer();
timer.Interval = 500;
timer.Elapsed += (sender, args) =>
{
List<LogModel> logs;
PopAll(out logs);
if (logs.Count > 0)
{
using (StreamWriter sw = File.AppendText(_path))
{
foreach (var log in logs)
{
string json = JsonSerializer.Serialize(log);
sw.WriteLine(json);
}
}
}
};
timer.Start();
}
private void Log(string type, string service, string traceId, string method, string userId, string message, string apiAddr)
{
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
_queue.Enqueue(new LogModel
{
Time = time,
Level = type,
Service = service,
Trace = traceId,
HttpMethod = method,
UserId = userId,
Message = message,
ApiAddr = ""
});
}
private void PopAll(out List<LogModel> logs)
{
// Lock을 써서 관리?
// Count만큼 할당받은 후에 처리?
logs = new List<LogModel>(_queue.Count);
while (_queue.TryDequeue(out LogModel? data))
{
logs.Add(data!);
}
}
public void LogInformation(string traceId, string method, string userId, string message, string apiAddr)
{
Log("INFO", _serviceName, traceId, method, userId, message, apiAddr);
}
public void LogWarning(string traceId, string method, string userId, string message, string apiAddr)
{
Log("WARN", _serviceName, traceId, method, userId, message, apiAddr);
}
public void LogDebug(string traceId, string method, string userId, string message, string apiAddr)
{
Log("DEBUG", _serviceName, traceId, method, userId, message, apiAddr);
}
public void LogError(string traceId, string method, string userId, string message, string apiAddr)
{
Log("ERROR", _serviceName, traceId, method, userId, message, apiAddr);
}
public void LogFatal(string traceId, string method, string userId, string message, string apiAddr)
{
Log("FATAL", _serviceName, traceId, method, userId, message, apiAddr);
}
public void LogVerbose(string traceId, string method, string userId, string message, string apiAddr)
{
Log("VERBOSE", _serviceName, traceId, method, userId, message, apiAddr);
}
}