forked from rickparrish/RMLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRMLog.cs
175 lines (155 loc) · 6.07 KB
/
RMLog.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
// Inspired by FleckLog: https://github.com/statianzo/Fleck/blob/master/src/Fleck/FleckLog.cs
namespace RandM.RMLib
{
public enum LogLevel
{
/// <summary>
/// Use for extremely detailed information that isn't likely to be useful for anybody but the developer
/// </summary>
Trace,
/// <summary>
/// Use for information that may be useful for a user to provide to a developer if there are problems
/// </summary>
Debug,
/// <summary>
/// Use for information that is "business as usual", which is likely to be output to screen
/// </summary>
Info,
/// <summary>
/// Use for information related to unexpected non-fatal conditions
/// </summary>
Warning,
/// <summary>
/// Use for information related to unexpected fatal conditions
/// </summary>
Error
}
public class RMLog
{
/// <summary>
/// Determine which events get raised to the application
/// </summary>
public static LogLevel Level = LogLevel.Info;
public static event EventHandler<RMLogEventArgs> Handler = null;
/// <summary>
/// Raises an Debug-level event
/// </summary>
/// <param name="message">The message to raise</param>
public static void Debug(string message)
{
if (Level <= LogLevel.Debug)
{
new RMLogEventArgs(LogLevel.Debug, message).Raise(null, Handler);
}
}
/// <summary>
/// Raises an Debug-level event, including information about what caused the exception.
/// Should be used for exceptions occurring in a library
/// </summary>
/// <param name="ex">The exception that occurred</param>
/// <param name="message">A message describing what happened</param>
public static void DebugException(Exception ex, string message) {
if (Level <= LogLevel.Debug) {
var Trace = new StackTrace(ex, true);
var Frame = Trace.GetFrame(0);
var Method = Frame.GetMethod();
message = string.Format("Message: {0}\r\nFile: {1}:{2},{3}\r\nMethod: {4}::{5}\r\nException: {6}",
message,
Frame.GetFileName(),
Frame.GetFileLineNumber(),
Frame.GetFileColumnNumber(),
Method.DeclaringType,
Method.Name,
ex.ToString());
new RMLogEventArgs(LogLevel.Debug, message).Raise(null, Handler);
}
}
/// <summary>
/// Raises an Error-level event
/// </summary>
/// <param name="message">The message to raise</param>
public static void Error(string message)
{
if (Level <= LogLevel.Error)
{
new RMLogEventArgs(LogLevel.Error, message).Raise(null, Handler);
}
}
/// <summary>
/// Raises an Error-level event, including information about what caused the exception
/// Should be used for exceptions occurring in a main application
/// </summary>
/// <param name="ex">The exception that occurred</param>
/// <param name="message">A message describing what happened</param>
public static void Exception(Exception ex, string message)
{
if (Level <= LogLevel.Error)
{
var Trace = new StackTrace(ex, true);
var Frame = Trace.GetFrame(0);
var Method = Frame.GetMethod();
message = string.Format("Message: {0}\r\nFile: {1}:{2},{3}\r\nMethod: {4}::{5}\r\nException: {6}",
message,
Frame.GetFileName(),
Frame.GetFileLineNumber(),
Frame.GetFileColumnNumber(),
Method.DeclaringType,
Method.Name,
(Level <= LogLevel.Debug) ? ex.ToString() : ex.Message);
new RMLogEventArgs(LogLevel.Error, message).Raise(null, Handler);
}
}
/// <summary>
/// Raises an Info-level event
/// </summary>
/// <param name="message">The message to raise</param>
public static void Info(string message)
{
if (Level <= LogLevel.Info)
{
new RMLogEventArgs(LogLevel.Info, message).Raise(null, Handler);
}
}
/// <summary>
/// Raises an Trace-level event
/// </summary>
/// <param name="message">The message to raise</param>
public static void Trace(string message)
{
if (Level <= LogLevel.Trace)
{
new RMLogEventArgs(LogLevel.Trace, message).Raise(null, Handler);
}
}
/// <summary>
/// Raises an Warning-level event
/// </summary>
/// <param name="message">The message to raise</param>
public static void Warning(string message)
{
if (Level <= LogLevel.Warning)
{
new RMLogEventArgs(LogLevel.Warning, message).Raise(null, Handler);
}
}
}
}