-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logging.cs
113 lines (94 loc) · 3.35 KB
/
Logging.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Decal.Adapter;
using Decal.Adapter.Wrappers;
// This function is very lightly modified from Virindi (virindi.net)
namespace TreeStats
{
public static class Logging
{
public static bool loggingState { get; set; }
public static string messagesFile { get; set; }
public static string errorLogFile { get; set; }
public static int fileSizeLimit = 102400; // 100 KB
internal static void Init(string _messages, string _errors)
{
try
{
loggingState = true;
messagesFile = _messages;
errorLogFile = _errors;
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void Destroy()
{
try
{
messagesFile = null;
errorLogFile = null;
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void LogMessage(string message)
{
try
{
if (loggingState == false)
{
return;
}
bool shouldAppend = true; // Default to appending log message
// Check file size and decide whether to append or not
FileInfo info = new FileInfo(messagesFile);
if (info.Exists && info.Length > fileSizeLimit)
{
shouldAppend = false;
}
System.IO.StreamWriter sw = new System.IO.StreamWriter(messagesFile, shouldAppend);
sw.WriteLine("[" + DateTime.Now.ToString() + "] " + message);
sw.Close();
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void LogError(Exception ex)
{
if (loggingState == false)
{
return;
}
bool shouldAppend = true; // Default to appending log message
// Check file size and decide whether to append or not
FileInfo info = new FileInfo(messagesFile);
if (info.Exists && info.Length > fileSizeLimit)
{
shouldAppend = false;
}
System.IO.StreamWriter sw = new System.IO.StreamWriter(errorLogFile, shouldAppend);
sw.WriteLine("============================================================================");
sw.WriteLine(DateTime.Now.ToString());
sw.WriteLine("Error: " + ex.Message);
sw.WriteLine("Source: " + ex.Source);
sw.WriteLine("Stack: " + ex.StackTrace);
if (ex.InnerException != null)
{
sw.WriteLine("Inner: " + ex.InnerException.Message);
sw.WriteLine("Inner Stack: " + ex.InnerException.StackTrace);
}
sw.WriteLine("============================================================================");
sw.WriteLine("");
sw.Close();
}
}
}