Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 3821274

Browse files
authored
Merge pull request #790 from ChrisSimmons/ExpandLogLevelExtension
Expand log level extension
2 parents 602e10e + a113909 commit 3821274

File tree

2 files changed

+329
-154
lines changed

2 files changed

+329
-154
lines changed

src/Microsoft.Extensions.Logging.Abstractions/LoggerExtensions.cs

Lines changed: 65 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ public static class LoggerExtensions
2828
/// <example>logger.LogDebug(0, exception, "Error while processing request from {Address}", address)</example>
2929
public static void LogDebug(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
3030
{
31-
if (logger == null)
32-
{
33-
throw new ArgumentNullException(nameof(logger));
34-
}
35-
36-
logger.Log(LogLevel.Debug, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
31+
logger.Log(LogLevel.Debug, eventId, exception, message, args);
3732
}
3833

3934
/// <summary>
@@ -46,12 +41,7 @@ public static void LogDebug(this ILogger logger, EventId eventId, Exception exce
4641
/// <example>logger.LogDebug(0, "Processing request from {Address}", address)</example>
4742
public static void LogDebug(this ILogger logger, EventId eventId, string message, params object[] args)
4843
{
49-
if (logger == null)
50-
{
51-
throw new ArgumentNullException(nameof(logger));
52-
}
53-
54-
logger.Log(LogLevel.Debug, eventId, new FormattedLogValues(message, args), null, _messageFormatter);
44+
logger.Log(LogLevel.Debug, eventId, message, args);
5545
}
5646

5747
/// <summary>
@@ -64,12 +54,7 @@ public static void LogDebug(this ILogger logger, EventId eventId, string message
6454
/// <example>logger.LogDebug(exception, "Error while processing request from {Address}", address)</example>
6555
public static void LogDebug(this ILogger logger, Exception exception, string message, params object[] args)
6656
{
67-
if (logger == null)
68-
{
69-
throw new ArgumentNullException(nameof(logger));
70-
}
71-
72-
logger.Log(LogLevel.Debug, 0, new FormattedLogValues(message, args), exception, _messageFormatter);
57+
logger.Log(LogLevel.Debug, exception, message, args);
7358
}
7459

7560
/// <summary>
@@ -81,12 +66,7 @@ public static void LogDebug(this ILogger logger, Exception exception, string mes
8166
/// <example>logger.LogDebug("Processing request from {Address}", address)</example>
8267
public static void LogDebug(this ILogger logger, string message, params object[] args)
8368
{
84-
if (logger == null)
85-
{
86-
throw new ArgumentNullException(nameof(logger));
87-
}
88-
89-
logger.Log(LogLevel.Debug, 0, new FormattedLogValues(message, args), null, _messageFormatter);
69+
logger.Log(LogLevel.Debug, message, args);
9070
}
9171

9272
//------------------------------------------TRACE------------------------------------------//
@@ -102,12 +82,7 @@ public static void LogDebug(this ILogger logger, string message, params object[]
10282
/// <example>logger.LogTrace(0, exception, "Error while processing request from {Address}", address)</example>
10383
public static void LogTrace(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
10484
{
105-
if (logger == null)
106-
{
107-
throw new ArgumentNullException(nameof(logger));
108-
}
109-
110-
logger.Log(LogLevel.Trace, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
85+
logger.Log(LogLevel.Trace, eventId, exception, message, args);
11186
}
11287

11388
/// <summary>
@@ -120,12 +95,7 @@ public static void LogTrace(this ILogger logger, EventId eventId, Exception exce
12095
/// <example>logger.LogTrace(0, "Processing request from {Address}", address)</example>
12196
public static void LogTrace(this ILogger logger, EventId eventId, string message, params object[] args)
12297
{
123-
if (logger == null)
124-
{
125-
throw new ArgumentNullException(nameof(logger));
126-
}
127-
128-
logger.Log(LogLevel.Trace, eventId, new FormattedLogValues(message, args), null, _messageFormatter);
98+
logger.Log(LogLevel.Trace, eventId, message, args);
12999
}
130100

131101
/// <summary>
@@ -138,12 +108,7 @@ public static void LogTrace(this ILogger logger, EventId eventId, string message
138108
/// <example>logger.LogTrace(exception, "Error while processing request from {Address}", address)</example>
139109
public static void LogTrace(this ILogger logger, Exception exception, string message, params object[] args)
140110
{
141-
if (logger == null)
142-
{
143-
throw new ArgumentNullException(nameof(logger));
144-
}
145-
146-
logger.Log(LogLevel.Trace, 0, new FormattedLogValues(message, args), exception, _messageFormatter);
111+
logger.Log(LogLevel.Trace, exception, message, args);
147112
}
148113

149114
/// <summary>
@@ -155,12 +120,7 @@ public static void LogTrace(this ILogger logger, Exception exception, string mes
155120
/// <example>logger.LogTrace("Processing request from {Address}", address)</example>
156121
public static void LogTrace(this ILogger logger, string message, params object[] args)
157122
{
158-
if (logger == null)
159-
{
160-
throw new ArgumentNullException(nameof(logger));
161-
}
162-
163-
logger.Log(LogLevel.Trace, 0, new FormattedLogValues(message, args), null, _messageFormatter);
123+
logger.Log(LogLevel.Trace, message, args);
164124
}
165125

166126
//------------------------------------------INFORMATION------------------------------------------//
@@ -176,12 +136,7 @@ public static void LogTrace(this ILogger logger, string message, params object[]
176136
/// <example>logger.LogInformation(0, exception, "Error while processing request from {Address}", address)</example>
177137
public static void LogInformation(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
178138
{
179-
if (logger == null)
180-
{
181-
throw new ArgumentNullException(nameof(logger));
182-
}
183-
184-
logger.Log(LogLevel.Information, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
139+
logger.Log(LogLevel.Information, eventId, exception, message, args);
185140
}
186141

187142
/// <summary>
@@ -194,12 +149,7 @@ public static void LogInformation(this ILogger logger, EventId eventId, Exceptio
194149
/// <example>logger.LogInformation(0, "Processing request from {Address}", address)</example>
195150
public static void LogInformation(this ILogger logger, EventId eventId, string message, params object[] args)
196151
{
197-
if (logger == null)
198-
{
199-
throw new ArgumentNullException(nameof(logger));
200-
}
201-
202-
logger.Log(LogLevel.Information, eventId, new FormattedLogValues(message, args), null, _messageFormatter);
152+
logger.Log(LogLevel.Information, eventId, message, args);
203153
}
204154

205155
/// <summary>
@@ -212,12 +162,7 @@ public static void LogInformation(this ILogger logger, EventId eventId, string m
212162
/// <example>logger.LogInformation(exception, "Error while processing request from {Address}", address)</example>
213163
public static void LogInformation(this ILogger logger, Exception exception, string message, params object[] args)
214164
{
215-
if (logger == null)
216-
{
217-
throw new ArgumentNullException(nameof(logger));
218-
}
219-
220-
logger.Log(LogLevel.Information, 0, new FormattedLogValues(message, args), exception, _messageFormatter);
165+
logger.Log(LogLevel.Information, exception, message, args);
221166
}
222167

223168
/// <summary>
@@ -229,12 +174,7 @@ public static void LogInformation(this ILogger logger, Exception exception, stri
229174
/// <example>logger.LogInformation("Processing request from {Address}", address)</example>
230175
public static void LogInformation(this ILogger logger, string message, params object[] args)
231176
{
232-
if (logger == null)
233-
{
234-
throw new ArgumentNullException(nameof(logger));
235-
}
236-
237-
logger.Log(LogLevel.Information, 0, new FormattedLogValues(message, args), null, _messageFormatter);
177+
logger.Log(LogLevel.Information, message, args);
238178
}
239179

240180
//------------------------------------------WARNING------------------------------------------//
@@ -250,12 +190,7 @@ public static void LogInformation(this ILogger logger, string message, params ob
250190
/// <example>logger.LogWarning(0, exception, "Error while processing request from {Address}", address)</example>
251191
public static void LogWarning(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
252192
{
253-
if (logger == null)
254-
{
255-
throw new ArgumentNullException(nameof(logger));
256-
}
257-
258-
logger.Log(LogLevel.Warning, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
193+
logger.Log(LogLevel.Warning, eventId, exception, message, args);
259194
}
260195

261196
/// <summary>
@@ -268,12 +203,7 @@ public static void LogWarning(this ILogger logger, EventId eventId, Exception ex
268203
/// <example>logger.LogWarning(0, "Processing request from {Address}", address)</example>
269204
public static void LogWarning(this ILogger logger, EventId eventId, string message, params object[] args)
270205
{
271-
if (logger == null)
272-
{
273-
throw new ArgumentNullException(nameof(logger));
274-
}
275-
276-
logger.Log(LogLevel.Warning, eventId, new FormattedLogValues(message, args), null, _messageFormatter);
206+
logger.Log(LogLevel.Warning, eventId, message, args);
277207
}
278208

279209
/// <summary>
@@ -286,12 +216,7 @@ public static void LogWarning(this ILogger logger, EventId eventId, string messa
286216
/// <example>logger.LogWarning(exception, "Error while processing request from {Address}", address)</example>
287217
public static void LogWarning(this ILogger logger, Exception exception, string message, params object[] args)
288218
{
289-
if (logger == null)
290-
{
291-
throw new ArgumentNullException(nameof(logger));
292-
}
293-
294-
logger.Log(LogLevel.Warning, 0, new FormattedLogValues(message, args), exception, _messageFormatter);
219+
logger.Log(LogLevel.Warning, exception, message, args);
295220
}
296221

297222
/// <summary>
@@ -303,12 +228,7 @@ public static void LogWarning(this ILogger logger, Exception exception, string m
303228
/// <example>logger.LogWarning("Processing request from {Address}", address)</example>
304229
public static void LogWarning(this ILogger logger, string message, params object[] args)
305230
{
306-
if (logger == null)
307-
{
308-
throw new ArgumentNullException(nameof(logger));
309-
}
310-
311-
logger.Log(LogLevel.Warning, 0, new FormattedLogValues(message, args), null, _messageFormatter);
231+
logger.Log(LogLevel.Warning, message, args);
312232
}
313233

314234
//------------------------------------------ERROR------------------------------------------//
@@ -324,12 +244,7 @@ public static void LogWarning(this ILogger logger, string message, params object
324244
/// <example>logger.LogError(0, exception, "Error while processing request from {Address}", address)</example>
325245
public static void LogError(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
326246
{
327-
if (logger == null)
328-
{
329-
throw new ArgumentNullException(nameof(logger));
330-
}
331-
332-
logger.Log(LogLevel.Error, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
247+
logger.Log(LogLevel.Error, eventId, exception, message, args);
333248
}
334249

335250
/// <summary>
@@ -342,12 +257,7 @@ public static void LogError(this ILogger logger, EventId eventId, Exception exce
342257
/// <example>logger.LogError(0, "Processing request from {Address}", address)</example>
343258
public static void LogError(this ILogger logger, EventId eventId, string message, params object[] args)
344259
{
345-
if (logger == null)
346-
{
347-
throw new ArgumentNullException(nameof(logger));
348-
}
349-
350-
logger.Log(LogLevel.Error, eventId, new FormattedLogValues(message, args), null, _messageFormatter);
260+
logger.Log(LogLevel.Error, eventId, message, args);
351261
}
352262

353263
/// <summary>
@@ -360,12 +270,7 @@ public static void LogError(this ILogger logger, EventId eventId, string message
360270
/// <example>logger.LogError(exception, "Error while processing request from {Address}", address)</example>
361271
public static void LogError(this ILogger logger, Exception exception, string message, params object[] args)
362272
{
363-
if (logger == null)
364-
{
365-
throw new ArgumentNullException(nameof(logger));
366-
}
367-
368-
logger.Log(LogLevel.Error, 0, new FormattedLogValues(message, args), exception, _messageFormatter);
273+
logger.Log(LogLevel.Error, exception, message, args);
369274
}
370275

371276
/// <summary>
@@ -377,12 +282,7 @@ public static void LogError(this ILogger logger, Exception exception, string mes
377282
/// <example>logger.LogError("Processing request from {Address}", address)</example>
378283
public static void LogError(this ILogger logger, string message, params object[] args)
379284
{
380-
if (logger == null)
381-
{
382-
throw new ArgumentNullException(nameof(logger));
383-
}
384-
385-
logger.Log(LogLevel.Error, 0, new FormattedLogValues(message, args), null, _messageFormatter);
285+
logger.Log(LogLevel.Error, message, args);
386286
}
387287

388288
//------------------------------------------CRITICAL------------------------------------------//
@@ -398,12 +298,7 @@ public static void LogError(this ILogger logger, string message, params object[]
398298
/// <example>logger.LogCritical(0, exception, "Error while processing request from {Address}", address)</example>
399299
public static void LogCritical(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
400300
{
401-
if (logger == null)
402-
{
403-
throw new ArgumentNullException(nameof(logger));
404-
}
405-
406-
logger.Log(LogLevel.Critical, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
301+
logger.Log(LogLevel.Critical, eventId, exception, message, args);
407302
}
408303

409304
/// <summary>
@@ -416,12 +311,7 @@ public static void LogCritical(this ILogger logger, EventId eventId, Exception e
416311
/// <example>logger.LogCritical(0, "Processing request from {Address}", address)</example>
417312
public static void LogCritical(this ILogger logger, EventId eventId, string message, params object[] args)
418313
{
419-
if (logger == null)
420-
{
421-
throw new ArgumentNullException(nameof(logger));
422-
}
423-
424-
logger.Log(LogLevel.Critical, eventId, new FormattedLogValues(message, args), null, _messageFormatter);
314+
logger.Log(LogLevel.Critical, eventId, message, args);
425315
}
426316

427317
/// <summary>
@@ -434,12 +324,7 @@ public static void LogCritical(this ILogger logger, EventId eventId, string mess
434324
/// <example>logger.LogCritical(exception, "Error while processing request from {Address}", address)</example>
435325
public static void LogCritical(this ILogger logger, Exception exception, string message, params object[] args)
436326
{
437-
if (logger == null)
438-
{
439-
throw new ArgumentNullException(nameof(logger));
440-
}
441-
442-
logger.Log(LogLevel.Critical, 0, new FormattedLogValues(message, args), exception, _messageFormatter);
327+
logger.Log(LogLevel.Critical, exception, message, args);
443328
}
444329

445330
/// <summary>
@@ -451,30 +336,64 @@ public static void LogCritical(this ILogger logger, Exception exception, string
451336
/// <example>logger.LogCritical("Processing request from {Address}", address)</example>
452337
public static void LogCritical(this ILogger logger, string message, params object[] args)
453338
{
454-
if (logger == null)
455-
{
456-
throw new ArgumentNullException(nameof(logger));
457-
}
339+
logger.Log(LogLevel.Critical, message, args);
340+
}
458341

459-
logger.Log(LogLevel.Critical, 0, new FormattedLogValues(message, args), null, _messageFormatter);
342+
/// <summary>
343+
/// Formats and writes a log message at the specified log level.
344+
/// </summary>
345+
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
346+
/// <param name="logLevel">Entry will be written on this level.</param>
347+
/// <param name="message">Format string of the log message.</param>
348+
/// <param name="args">An object array that contains zero or more objects to format.</param>
349+
public static void Log(this ILogger logger, LogLevel logLevel, string message, params object[] args)
350+
{
351+
logger.Log(logLevel, 0, null, message, args);
460352
}
461353

354+
/// <summary>
355+
/// Formats and writes a log message at the specified log level.
356+
/// </summary>
357+
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
358+
/// <param name="logLevel">Entry will be written on this level.</param>
359+
/// <param name="eventId">The event id associated with the log.</param>
360+
/// <param name="message">Format string of the log message.</param>
361+
/// <param name="args">An object array that contains zero or more objects to format.</param>
362+
public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string message, params object[] args)
363+
{
364+
logger.Log(logLevel, eventId, null, message, args);
365+
}
462366

463367
/// <summary>
464-
/// Formats and writes a log message on specified log level.
368+
/// Formats and writes a log message at the specified log level.
465369
/// </summary>
466370
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
467371
/// <param name="logLevel">Entry will be written on this level.</param>
372+
/// <param name="exception">The exception to log.</param>
468373
/// <param name="message">Format string of the log message.</param>
469374
/// <param name="args">An object array that contains zero or more objects to format.</param>
470-
public static void Log(this ILogger logger, LogLevel logLevel, string message, params object[] args)
375+
public static void Log(this ILogger logger, LogLevel logLevel, Exception exception, string message, params object[] args)
376+
{
377+
logger.Log(logLevel, 0, exception, message, args);
378+
}
379+
380+
/// <summary>
381+
/// Formats and writes a log message at the specified log level.
382+
/// </summary>
383+
/// <param name="logger">The <see cref="ILogger"/> to write to.</param>
384+
/// <param name="logLevel">Entry will be written on this level.</param>
385+
/// <param name="eventId">The event id associated with the log.</param>
386+
/// <param name="exception">The exception to log.</param>
387+
/// <param name="message">Format string of the log message.</param>
388+
/// <param name="args">An object array that contains zero or more objects to format.</param>
389+
public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, string message, params object[] args)
471390
{
472391
if (logger == null)
473392
{
474393
throw new ArgumentNullException(nameof(logger));
475394
}
476395

477-
logger.Log(logLevel, 0, new FormattedLogValues(message, args), null, _messageFormatter);
396+
logger.Log(logLevel, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);
478397
}
479398

480399
//------------------------------------------Scope------------------------------------------//

0 commit comments

Comments
 (0)