-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
87 lines (83 loc) · 3.01 KB
/
Copy pathLogger.cs
File metadata and controls
87 lines (83 loc) · 3.01 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public enum LogMinLevel { Debug, Info, Error };
public enum LogTarget { Console, File, Both };
public class Logger
{
private string target_filename;
private LogMinLevel minLevel;
private LogTarget target;
public Logger(LogTarget target, string target_filename, LogMinLevel minLevel)
{
this.target_filename = target_filename;
this.minLevel = minLevel;
this.target = target;
}
public Logger(string settings_filename)
{
StreamReader sr = new StreamReader(settings_filename);
Dictionary<string, string> settings = new Dictionary<string, string>();
settings["TargetFileName"] = sr.ReadLine().Split('=')[1];
settings["MinLevel"] = sr.ReadLine().Split('=')[1];
settings["Target"] = sr.ReadLine().Split('=')[1];
this.target_filename = settings["TargetFileName"];
if (settings["MinLevel"] == "Debug") { this.minLevel = LogMinLevel.Debug; }
if (settings["MinLevel"] == "Info") { this.minLevel = LogMinLevel.Info; }
if (settings["MinLevel"] == "Error") { this.minLevel = LogMinLevel.Error; }
if (settings["Target"] == "file") { this.target = LogTarget.File; }
if (settings["Target"] == "console") { this.target = LogTarget.Console; }
if (settings["Target"] == "both") { this.target = LogTarget.Both; }
}
public void Debug(string message)
{
if ((int)this.minLevel == 0)
{
string line = String.Format("{0} {1} Debug {2}", DateTime.Now.ToShortDateString(),
DateTime.Now.ToLongTimeString(), message);
if (this.target == LogTarget.File || this.target == LogTarget.Both)
{
StreamWriter sw = new StreamWriter(this.target_filename, true);
sw.WriteLine(line);
sw.Close();
}
if (this.target == LogTarget.Console || this.target == LogTarget.Both) { Console.WriteLine(line); }
}
}
public void Info(string message)
{
if ((int)this.minLevel <= 1)
{
string line = String.Format("{0} {1} Info {2}", DateTime.Now.ToShortDateString(),
DateTime.Now.ToLongTimeString(), message);
if (this.target == LogTarget.File || this.target == LogTarget.Both)
{
StreamWriter sw = new StreamWriter(this.target_filename, true);
sw.WriteLine(line);
sw.Close();
}
if (this.target == LogTarget.Console || this.target == LogTarget.Both) { Console.WriteLine(line); }
}
}
public void Error(string message)
{
if ((int)this.minLevel <= 2)
{
string line = String.Format("{0} {1} Error {2}", DateTime.Now.ToShortDateString(),
DateTime.Now.ToLongTimeString(), message);
if (this.target == LogTarget.File || this.target == LogTarget.Both)
{
StreamWriter sw = new StreamWriter(this.target_filename, true);
sw.WriteLine(line);
sw.Close();
}
if (this.target == LogTarget.Console || this.target == LogTarget.Both) { Console.WriteLine(line); }
}
}
}
}