-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
55 lines (46 loc) · 1.63 KB
/
Logger.cs
File metadata and controls
55 lines (46 loc) · 1.63 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
using System;
using System.Diagnostics;
using System.Reflection;
namespace TaskBoardWf
{
internal static class Logger
{
public static TraceLevel LogLevel { get; set; }
static Logger()
{
Trace.Listeners.Clear();
var logFileName = Program.appSettings.LogFileName ?? "log.txt";
Trace.Listeners.Add(new CustomTraceListener(logFileName));
var logLevel = Program.appSettings.LogLevel ?? "Error";
LogLevel = (TraceLevel)Enum.Parse(typeof(TraceLevel), logLevel, true);
Trace.AutoFlush = true;
}
public static void LogInfo(string message)
{
if (LogLevel >= TraceLevel.Info) {
Log(message, TraceEventType.Information);
}
}
public static void LogWarning(string message)
{
if (LogLevel >= TraceLevel.Warning) {
Log(message, TraceEventType.Warning);
}
}
public static void LogError(string message)
{
if (LogLevel >= TraceLevel.Error) {
Log(message, TraceEventType.Error);
}
}
private static void Log(string message, TraceEventType eventType)
{
StackFrame frame = new StackFrame(2, true);
MethodBase method = frame.GetMethod();
string className = method.DeclaringType.FullName;
string methodName = method.Name;
string formattedMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {eventType} - {className}.{methodName} - {message}";
Trace.WriteLine(formattedMessage);
}
}
}