-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
112 lines (95 loc) · 2.83 KB
/
Logger.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace CaledosLab.Portable.Logging
{
public static class Logger
{
private static int _max = 500;
/// <summary>
/// max number of line logged by the system
/// </summary>
public static int MaxSize
{
get { return _max;}
set { _max = value;}
}
private static bool _enabled = false;
/// <summary>
/// enable/disable store logging
/// </summary>
public static bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
private static List<string> buffer { get; set; }
public static void WriteLine(Exception e)
{
WriteLine ("EXCEPTION {0} {1} STACK TRACE {2}", e.Message, e.InnerException != null ? " HAS INNER EXCEPTION" : "", e.StackTrace);
if (e.InnerException != null)
{
WriteLine(e.InnerException);
}
}
public static void WriteLine(string format, params object[] args)
{
string s = string.Format(format, args);
WriteLine(s);
}
public static void WriteLine(string line)
{
if (Enabled)
{
StringBuilder sb = new StringBuilder();
sb.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
sb.Append(" ");
sb.Append(System.Threading.Thread.CurrentThread.ManagedThreadId);
sb.Append(" ");
sb.Append(line);
if (buffer == null)
{
buffer = new System.Collections.Generic.List<string>();
}
buffer.Add(sb.ToString());
while (buffer.Count() > MaxSize)
{
buffer.RemoveAt(0);
}
System.Diagnostics.Debug.WriteLine(sb);
}
}
public static void Load(StreamReader stream)
{
buffer = new List<string>();
while (!stream.EndOfStream)
{
buffer.Add(stream.ReadLine());
}
}
public static void Save(StreamWriter stream)
{
if (buffer != null)
{
foreach (string s in buffer)
{
stream.WriteLine(s);
}
}
}
public static string GetStoredLog()
{
StringBuilder sb = new StringBuilder();
if (buffer != null)
{
foreach (string s in buffer)
{
sb.AppendLine(s);
}
}
return sb.ToString();
}
}
}