-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLogToFile.cs
More file actions
124 lines (115 loc) · 3.85 KB
/
Copy pathLogToFile.cs
File metadata and controls
124 lines (115 loc) · 3.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Logging
{
public class LogToFile
{
private readonly string ErrorPath;
private readonly string InfoPath;
private readonly string WarningPath;
private readonly string FatalPath;
private readonly string SuccessPath;
public LogToFile()
{
using (var file = new StreamReader("LogConfig.cfg"))
{
string tempLine;
while((tempLine = file.ReadLine()) != null)
{
tempLine = tempLine.Trim();
var index = tempLine.IndexOf('=');
if(index < 0)
{
continue;
}
var tempSymbols = tempLine.Substring(index + 1);
var tempVar = tempLine.Substring(0, index);
tempSymbols = tempSymbols.Trim();
tempVar = tempVar.Trim();
switch (tempVar)
{
case "ErrorPath":
ErrorPath = tempSymbols;
break;
case "InfoPath":
InfoPath = tempSymbols;
break;
case "WarningPath":
WarningPath = tempSymbols;
break;
case "FatalPath":
FatalPath = tempSymbols;
break;
case "SuccessPath":
SuccessPath = tempSymbols;
break;
}
}
}
}
public LogToFile(string errorPath, string infoPath, string warningPath, string fatalPath, string successPath)
{
ErrorPath = errorPath;
InfoPath = infoPath;
WarningPath = warningPath;
FatalPath = fatalPath;
SuccessPath = successPath;
}
public void Log(string path, string type, string message)
{
var msg = $"{DateTime.Now} [{type}] : {message}";
using var file = new StreamWriter(path, true, Encoding.UTF8);
file.WriteLine(msg);
}
public void Info(string message)
{
Log(InfoPath,"INFO ", $"{message}");
}
public void Error(string message)
{
Log(ErrorPath,"ERROR", $"{message}");
}
public void Warning(string message)
{
Log(WarningPath, "WARNING", $"{message}");
}
public void Fatal(string message)
{
Log(FatalPath, "FATAL", $"{message}");
}
public void Success(string message)
{
Log(InfoPath, "SUCCESS", $"{message}");
}
public async Task LogAsync(string path, string type, string message)
{
var msg = $"{DateTime.Now} [{type}] : {message}";
using (var file = new StreamWriter(path, true, Encoding.UTF8))
{
await file.WriteLineAsync(msg);
}
}
public async Task InfoAsync(string message)
{
await LogAsync(InfoPath, "INFO ", message);
}
public async Task ErrorAsync(string message)
{
await LogAsync(ErrorPath, "ERROR", message);
}
public async Task WarningAsync(string message)
{
await LogAsync(WarningPath, "WARNING", message);
}
public async Task FatalAsync(string message)
{
await LogAsync(FatalPath, "FATAL", message);
}
public async Task SuccessAsync(string message)
{
await LogAsync(InfoPath, "SUCCESS", message);
}
}
}