-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cs
More file actions
54 lines (47 loc) · 1.74 KB
/
logger.cs
File metadata and controls
54 lines (47 loc) · 1.74 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
using System;
using System.IO;
namespace Log
{
enum LogFlags {StdoutOnly, FileOnly, FileAndStdout};
class Logger { // Basic logging class
public String filename = "log.txt"; // Filename for the log
private LogFlags flags = LogFlags.StdoutOnly;
private String name = "Logger";
public Logger(String name, LogFlags flag = LogFlags.StdoutOnly, String filename="log.txt")
{
this.flags = flag;
this.filename = filename;
this.name = name;
}
public void info(String output)
{
this.write("["+name+": "+DateTime.Now.ToShortTimeString()+" - Info]: "+output, ConsoleColor.Blue);
}
public void warning(String output)
{
this.write("["+name+": "+DateTime.Now.ToShortTimeString()+" - Warning]: "+output, ConsoleColor.Yellow);
}
public void error(String output)
{
this.write("["+name+": "+DateTime.Now.ToShortTimeString()+" - Error]: "+output, ConsoleColor.Red);
}
private void write(String output, Nullable<ConsoleColor> color = null)
{
if (this.flags == LogFlags.FileAndStdout || this.flags == LogFlags.StdoutOnly)
{
if (color != null) {
Console.ForegroundColor = (ConsoleColor)color;
}
Console.WriteLine(output);
Console.ResetColor();
}
if (this.flags == LogFlags.FileAndStdout || this.flags == LogFlags.FileOnly)
{
using (StreamWriter sw = File.AppendText(this.filename))
{
sw.WriteLine(output);
}
}
}
}
}