forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogExtensions.cs
More file actions
251 lines (217 loc) · 7.63 KB
/
LogExtensions.cs
File metadata and controls
251 lines (217 loc) · 7.63 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
namespace ScriptCs.Contracts
{
using System;
public static class LogExtensions
{
public static bool IsDebugEnabled(this ILog logger)
{
Guard.AgainstNullArgument("logger", logger);
return logger.Log(LogLevel.Debug);
}
public static bool IsErrorEnabled(this ILog logger)
{
Guard.AgainstNullArgument("logger", logger);
return logger.Log(LogLevel.Error);
}
public static bool IsFatalEnabled(this ILog logger)
{
Guard.AgainstNullArgument("logger", logger);
return logger.Log(LogLevel.Fatal);
}
public static bool IsInfoEnabled(this ILog logger)
{
Guard.AgainstNullArgument("logger", logger);
return logger.Log(LogLevel.Info);
}
public static bool IsTraceEnabled(this ILog logger)
{
Guard.AgainstNullArgument("logger", logger);
return logger.Log(LogLevel.Trace);
}
public static bool IsWarnEnabled(this ILog logger)
{
Guard.AgainstNullArgument("logger", logger);
return logger.Log(LogLevel.Warn);
}
public static void Debug(this ILog logger, Func<string> messageFunc)
{
Guard.AgainstNullArgument("logger", logger);
logger.Log(LogLevel.Debug, messageFunc);
}
public static void Debug(this ILog logger, string message)
{
if (logger.IsDebugEnabled())
{
logger.Log(LogLevel.Debug, message.AsFunc());
}
}
public static void DebugFormat(this ILog logger, string message, params object[] args)
{
if (logger.IsDebugEnabled())
{
logger.LogFormat(LogLevel.Debug, message, args);
}
}
public static void DebugException(this ILog logger, string message, Exception exception)
{
if (logger.IsDebugEnabled())
{
logger.Log(LogLevel.Debug, message.AsFunc(), exception);
}
}
public static void DebugException(
this ILog logger, string message, Exception exception, params object[] formatParams)
{
if (logger.IsDebugEnabled())
{
logger.Log(LogLevel.Debug, message.AsFunc(), exception, formatParams);
}
}
public static void Error(this ILog logger, Func<string> messageFunc)
{
logger.Log(LogLevel.Error, messageFunc);
}
public static void Error(this ILog logger, string message)
{
if (logger.IsErrorEnabled())
{
logger.Log(LogLevel.Error, message.AsFunc());
}
}
public static void ErrorFormat(this ILog logger, string message, params object[] args)
{
if (logger.IsErrorEnabled())
{
logger.LogFormat(LogLevel.Error, message, args);
}
}
public static void ErrorException(
this ILog logger, string message, Exception exception, params object[] formatParams)
{
if (logger.IsErrorEnabled())
{
logger.Log(LogLevel.Error, message.AsFunc(), exception, formatParams);
}
}
public static void Fatal(this ILog logger, Func<string> messageFunc)
{
logger.Log(LogLevel.Fatal, messageFunc);
}
public static void Fatal(this ILog logger, string message)
{
if (logger.IsFatalEnabled())
{
logger.Log(LogLevel.Fatal, message.AsFunc());
}
}
public static void FatalFormat(this ILog logger, string message, params object[] args)
{
if (logger.IsFatalEnabled())
{
logger.LogFormat(LogLevel.Fatal, message, args);
}
}
public static void FatalException(
this ILog logger, string message, Exception exception, params object[] formatParams)
{
if (logger.IsFatalEnabled())
{
logger.Log(LogLevel.Fatal, message.AsFunc(), exception, formatParams);
}
}
public static void Info(this ILog logger, Func<string> messageFunc)
{
Guard.AgainstNullArgument("logger", logger);
logger.Log(LogLevel.Info, messageFunc);
}
public static void Info(this ILog logger, string message)
{
if (logger.IsInfoEnabled())
{
logger.Log(LogLevel.Info, message.AsFunc());
}
}
public static void InfoFormat(this ILog logger, string message, params object[] args)
{
if (logger.IsInfoEnabled())
{
logger.LogFormat(LogLevel.Info, message, args);
}
}
public static void InfoException(
this ILog logger, string message, Exception exception, params object[] formatParams)
{
if (logger.IsInfoEnabled())
{
logger.Log(LogLevel.Info, message.AsFunc(), exception, formatParams);
}
}
public static void Trace(this ILog logger, Func<string> messageFunc)
{
Guard.AgainstNullArgument("logger", logger);
logger.Log(LogLevel.Trace, messageFunc);
}
public static void Trace(this ILog logger, string message)
{
if (logger.IsTraceEnabled())
{
logger.Log(LogLevel.Trace, message.AsFunc());
}
}
public static void TraceFormat(this ILog logger, string message, params object[] args)
{
if (logger.IsTraceEnabled())
{
logger.LogFormat(LogLevel.Trace, message, args);
}
}
public static void TraceException(
this ILog logger, string message, Exception exception, params object[] formatParams)
{
if (logger.IsTraceEnabled())
{
logger.Log(LogLevel.Trace, message.AsFunc(), exception, formatParams);
}
}
public static void Warn(this ILog logger, Func<string> messageFunc)
{
Guard.AgainstNullArgument("logger", logger);
logger.Log(LogLevel.Warn, messageFunc);
}
public static void Warn(this ILog logger, string message)
{
if (logger.IsWarnEnabled())
{
logger.Log(LogLevel.Warn, message.AsFunc());
}
}
public static void WarnFormat(this ILog logger, string message, params object[] args)
{
if (logger.IsWarnEnabled())
{
logger.LogFormat(LogLevel.Warn, message, args);
}
}
public static void WarnException(
this ILog logger, string message, Exception exception, params object[] formatParams)
{
if (logger.IsWarnEnabled())
{
logger.Log(LogLevel.Warn, message.AsFunc(), exception, formatParams);
}
}
private static void LogFormat(this ILog logger, LogLevel logLevel, string message, params object[] args)
{
logger.Log(logLevel, message.AsFunc(), null, args);
}
// Avoid the closure allocation, see https://gist.github.com/AArnott/d285feef75c18f6ecd2b
private static Func<T> AsFunc<T>(this T value) where T : class
{
return value.Return;
}
private static T Return<T>(this T value)
{
return value;
}
}
}