-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConsoleLogger.cs
More file actions
115 lines (102 loc) · 4.08 KB
/
ConsoleLogger.cs
File metadata and controls
115 lines (102 loc) · 4.08 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
using System;
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace APIMatic.Core.Utilities.Logger
{
/// <summary>
/// Represents a logger implementation that writes log messages to the console.
/// </summary>
public class ConsoleLogger : ILogger
{
/// <summary>
/// Returns the shared instance of <see cref="T:Microsoft.Extensions.Logging.Abstractions.NullLogger" />.
/// </summary>
public static ConsoleLogger Instance { get; } = new ConsoleLogger();
/// <summary>
/// Initializes a new instance of the <see cref="T:Microsoft.Extensions.Logging.Abstractions.NullLogger" /> class.
/// </summary>
private ConsoleLogger()
{
}
/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state) =>
NullLogger.Instance.BeginScope(state);
/// <inheritdoc />
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel)) return;
LogEntry<TState> logEntry = new LogEntry<TState>(logLevel, "Console", eventId, state, exception, formatter);
var logString = WriteLogString(logEntry);
Console.WriteLine(logString);
}
/// <summary>
/// Writes the log message to a string.
/// </summary>
/// <typeparam name="TState">The type of the log state.</typeparam>
/// <param name="logEntry">The log entry.</param>
/// <returns>The log message as a string.</returns>
private static string WriteLogString<TState>(LogEntry<TState> logEntry)
{
var stringWriter = new StringWriter();
var message = logEntry.Formatter(logEntry.State, logEntry.Exception);
if (logEntry.Exception == null && message == null) return string.Empty;
var logLevelString = GetLogLevelString(logEntry.LogLevel);
stringWriter.Write(logLevelString);
stringWriter.Write(':');
stringWriter.Write(' ');
stringWriter.Write(logEntry.Category);
stringWriter.Write('[');
stringWriter.Write(logEntry.EventId.Id.ToString());
stringWriter.Write(']');
// scope information
if (!string.IsNullOrEmpty(message))
{
stringWriter.Write(' ');
stringWriter.Write(message);
}
return WriteException(logEntry, stringWriter);
}
private static string WriteException<TState>(LogEntry<TState> logEntry, StringWriter stringWriter)
{
if (logEntry.Exception == null) return stringWriter.ToString();
var exceptionMessage = logEntry.Exception.ToString();
stringWriter.Write(' ');
stringWriter.Write(exceptionMessage);
return stringWriter.ToString();
}
/// <inheritdoc />
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
/// <summary>
/// Retrieves the log level string representation.
/// </summary>
/// <param name="logLevel">The log level.</param>
/// <returns>The string representation of the log level.</returns>
private static string GetLogLevelString(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Trace:
return "trce";
case LogLevel.Debug:
return "dbug";
case LogLevel.Information:
return "info";
case LogLevel.Warning:
return "warn";
case LogLevel.Error:
return "fail";
case LogLevel.Critical:
return "crit";
case LogLevel.None:
default:
throw new ArgumentOutOfRangeException(nameof(logLevel));
}
}
}
}