-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
167 lines (135 loc) · 5.25 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.IO;
namespace nk.logger.csv
{
public class Logger
{
#region [Declarations]
private readonly LoggerConfig config;
#endregion [Declarations]
#region [Initialization]
/// <summary>
/// Initiate an instance of Logger class with default config
/// </summary>
public Logger()
{
config = new LoggerConfig();
InitializeLogFile();
}
/// <summary>
/// Initiate an instance of Logger class with custom config
/// </summary>
/// <param name="loggerConfig">Custom configuration</param>
public Logger(LoggerConfig loggerConfig)
{
config = loggerConfig;
InitializeLogFile();
}
/// <summary>
/// Checks if a file previously exists, if not creates the file with headings
/// </summary>
private void InitializeLogFile()
{
// Creates folders is not present, if present it is ignored automatically
Directory.CreateDirectory(config.GetFullFilePath());
// Creating new file is there is none
if (!File.Exists(config.GetFullFileName()))
{
WriteLine("Time,Error Level,Error Message", false);
}
}
#endregion [Initialization]
#region [Public Logger functions]
#region [Exception Logs]
/// <summary>
/// Log an <see cref="Exception"/> as DEBUG
/// </summary>
/// <param name="ex">Exception to log</param>
public void Debug(Exception ex) => WriteLog(LogLevels.DEBUG, ex?.ToString());
/// <summary>
/// Log an <see cref="Exception"/> as ERROR
/// </summary>
/// <param name="ex">Exception to log</param>
public void Error(Exception ex) => WriteLog(LogLevels.ERROR, ex?.ToString());
/// <summary>
/// Log an <see cref="Exception"/> as Fatal
/// </summary>
/// <param name="ex">Exception to log</param>
public void Fatal(Exception ex) => WriteLog(LogLevels.FATAL, ex?.ToString());
#endregion [Exception Logs]
#region [Error message logs]
/// <summary>
/// Log a DEBUG message
/// </summary>
/// <param name="text">Message to log</param>
public void Debug(string text) => WriteLog(LogLevels.DEBUG, text);
/// <summary>
/// Log an ERROR message
/// </summary>
/// <param name="text">Message to log</param>
public void Error(string text) => WriteLog(LogLevels.ERROR, text);
/// <summary>
/// Log a FATAL ERROR message
/// </summary>
/// <param name="text">Message to log</param>
public void Fatal(string text) => WriteLog(LogLevels.FATAL, text);
/// <summary>
/// Log an INFO message
/// </summary>
/// <param name="text">Message to log</param>
public void Info(string text) => WriteLog(LogLevels.INFO, text);
/// <summary>
/// Log a TRACE message
/// </summary>
/// <param name="text">Message to log</param>
public void Trace(string text) => WriteLog(LogLevels.TRACE, text);
/// <summary>
/// Log a WARNING message
/// </summary>
/// <param name="text">Message to log</param>
public void Warning(string text) => WriteLog(LogLevels.WARNING, text);
#endregion [Error message logs]
#endregion [Public Logger functions]
#region [Write functions]
/// <summary>
/// Adds date time, loglevel to error text and calls <see cref="WriteLine(string, bool)"/>
/// </summary>
private void WriteLog(string logLevel, string text)
{
if (!string.IsNullOrEmpty(text))
{
text = text.Replace(',', config.GetReplacementValue());
text = RemoveLineEndings(text);
string pretext = $"{DateTime.Now.ToString(config.GetDateTimeFormat())},{logLevel},";
WriteLine(pretext + text);
}
}
/// <summary>
/// Writes the input text into log file
/// </summary>
/// <param name="text">Input text</param>
/// <param name="append">Whether to append the old text or replace it</param>
private void WriteLine(string text, bool append = true)
{
using (var writer = new StreamWriter(config.GetFullFileName(), append, config.encoding))
{
writer.WriteLine(text);
}
}
#endregion [Write Functions]
#region [Helper Functions]
/// <summary>
/// Removes all line endings from the text
/// Ref: https://stackoverflow.com/a/6765676/5407188
/// </summary>
private static string RemoveLineEndings(string value)
{
return value.Replace("\r\n", string.Empty)
.Replace("\n", string.Empty)
.Replace("\r", string.Empty)
.Replace(((char)0x2028).ToString(), string.Empty) // line separator
.Replace(((char)0x2029).ToString(), string.Empty); // paragraph separator
}
#endregion [Helper Functions]
}
}