-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathLoggingExtensions.cs
114 lines (96 loc) · 4.67 KB
/
LoggingExtensions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.Testing.Platform.Logging;
/// <summary>
/// A set of extension methods for <see cref="ILogger"/>.
/// </summary>
public static class LoggingExtensions
{
internal static readonly Func<string, Exception?, string> Formatter =
(state, exception) =>
exception is not null
#pragma warning disable RS0030 // Do not use banned APIs
? $"{state}{Environment.NewLine}------Exception detail------{Environment.NewLine}{exception}"
#pragma warning restore RS0030 // Do not use banned APIs
: state;
/// <summary>
/// Asynchronously logs a message with <see cref="LogLevel.Trace"/>.
/// </summary>
public static Task LogTraceAsync(this ILogger logger, string message)
=> logger.LogAsync(LogLevel.Trace, message, null, Formatter);
/// <summary>
/// Asynchronously logs a message with <see cref="LogLevel.Debug"/>.
/// </summary>
public static Task LogDebugAsync(this ILogger logger, string message)
=> logger.LogAsync(LogLevel.Debug, message, null, Formatter);
/// <summary>
/// Asynchronously logs a message with <see cref="LogLevel.Information"/>.
/// </summary>
public static Task LogInformationAsync(this ILogger logger, string message)
=> logger.LogAsync(LogLevel.Information, message, null, Formatter);
/// <summary>
/// Asynchronously logs a message with <see cref="LogLevel.Warning"/>.
/// </summary>
public static Task LogWarningAsync(this ILogger logger, string message)
=> logger.LogAsync(LogLevel.Warning, message, null, Formatter);
/// <summary>
/// Asynchronously logs a message with <see cref="LogLevel.Error"/>.
/// </summary>
public static Task LogErrorAsync(this ILogger logger, string message)
=> logger.LogAsync(LogLevel.Error, message, null, Formatter);
/// <summary>
/// Asynchronously logs a message with <see cref="LogLevel.Error"/> which is associated with an exception.
/// </summary>
public static Task LogErrorAsync(this ILogger logger, string message, Exception ex)
=> logger.LogAsync(LogLevel.Error, message, ex, Formatter);
/// <summary>
/// Asynchronously logs an exception with <see cref="LogLevel.Error"/>.
/// </summary>
public static Task LogErrorAsync(this ILogger logger, Exception ex)
=> logger.LogAsync(LogLevel.Error, ex.ToString(), null, Formatter);
/// <summary>
/// Asynchronously logs a message with <see cref="LogLevel.Critical"/>.
/// </summary>
public static Task LogCriticalAsync(this ILogger logger, string message)
=> logger.LogAsync(LogLevel.Critical, message, null, Formatter);
/// <summary>
/// Logs a message with <see cref="LogLevel.Trace"/>.
/// </summary>
public static void LogTrace(this ILogger logger, string message)
=> logger.Log(LogLevel.Trace, message, null, Formatter);
/// <summary>
/// Logs a message with <see cref="LogLevel.Debug"/>.
/// </summary>
public static void LogDebug(this ILogger logger, string message)
=> logger.Log(LogLevel.Debug, message, null, Formatter);
/// <summary>
/// Logs a message with <see cref="LogLevel.Information"/>.
/// </summary>
public static void LogInformation(this ILogger logger, string message)
=> logger.Log(LogLevel.Information, message, null, Formatter);
/// <summary>
/// Logs a message with <see cref="LogLevel.Warning"/>.
/// </summary>
public static void LogWarning(this ILogger logger, string message)
=> logger.Log(LogLevel.Warning, message, null, Formatter);
/// <summary>
/// Logs a message with <see cref="LogLevel.Error"/>.
/// </summary>
public static void LogError(this ILogger logger, string message)
=> logger.Log(LogLevel.Error, message, null, Formatter);
/// <summary>
/// Logs a message with <see cref="LogLevel.Error"/> which is associated with an exception.
/// </summary>
public static void LogError(this ILogger logger, string message, Exception ex)
=> logger.Log(LogLevel.Error, message, ex, Formatter);
/// <summary>
/// Logs an exception with <see cref="LogLevel.Error"/>.
/// </summary>
public static void LogError(this ILogger logger, Exception ex)
=> logger.Log(LogLevel.Error, ex.ToString(), null, Formatter);
/// <summary>
/// Logs a message with <see cref="LogLevel.Critical"/>.
/// </summary>
public static void LogCritical(this ILogger logger, string message)
=> logger.Log(LogLevel.Critical, message, null, Formatter);
}