-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
75 lines (69 loc) · 2.29 KB
/
Log.cs
File metadata and controls
75 lines (69 loc) · 2.29 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CharacterBuilderLoader
{
public static class Log
{
static Log()
{
LogFile = FileManager.BasePath + "/log.txt";
if (File.Exists(LogFile))
File.WriteAllText(LogFile, ""); // Resets the contents without actually deleting the file.
}
public static string LogFile { get; private set; }
public static bool ErrorLogged { get; private set; }
public static bool VerboseMode { get; set; }
public static void Debug(string msg)
{
if (VerboseMode)
{
Console.WriteLine("Debug: " + msg);
writeToFile("Debug: " + msg);
}
}
public static void Info(string msg)
{
Console.WriteLine(msg);
writeToFile("INFO: " + msg);
}
public static void Error(string msg)
{
ErrorLogged = true;
string taggedMsg = "ERROR: " + msg;
Console.WriteLine(taggedMsg);
writeToFile(taggedMsg);
}
private static void writeToFile(string taggedMsg)
{
using (StreamWriter sw = new StreamWriter(
new FileStream(LogFile, FileMode.Append)))
{
sw.WriteLine(DateTime.Now.ToString() + " - " + taggedMsg);
}
}
public static void Error(string msg, Exception e)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(msg);
Exception current = e;
int tabCount = 0;
while (current != null)
{
sb.Append("Inner Exception: ");
sb.AppendLine("".PadLeft(tabCount * 3, ' ') + current.Message);
if (VerboseMode)
{
sb.AppendLine("".PadLeft(tabCount * 3, ' ') + current.StackTrace);
sb.AppendLine("".PadLeft(80, '-'));
sb.AppendLine();
}
current = current.InnerException;
tabCount++;
}
Error(sb.ToString());
}
}
}