-
Notifications
You must be signed in to change notification settings - Fork 120
/
Log4NetLogger.cs
148 lines (137 loc) · 5.22 KB
/
Log4NetLogger.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
namespace Microsoft.Extensions.Logging
{
using System;
using log4net;
/// <summary>
/// The log4net logger class.
/// </summary>
public class Log4NetLogger : ILogger
{
/// <summary>
/// The log.
/// </summary>
private readonly ILog log;
/// <summary>
/// The formatter when logging an exception.
/// </summary>
private Func<object, Exception, string> exceptionDetailsFormatter;
/// <summary>
/// Initializes a new instance of the <see cref="Log4NetLogger"/> class.
/// </summary>
/// <param name="loggerRepository">The repository name.</param>
/// <param name="name">The logger's name.</param>
public Log4NetLogger(string loggerRepository, string name)
{
this.log = LogManager.GetLogger(loggerRepository, name);
}
/// <summary>
/// Begins a logical operation scope.
/// </summary>
/// <typeparam name="TState">The type of the state.</typeparam>
/// <param name="state">The identifier for the scope.</param>
/// <returns>
/// An IDisposable that ends the logical operation scope on dispose.
/// </returns>
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
/// <summary>
/// Determines whether the logging level is enabled.
/// </summary>
/// <param name="logLevel">The log level.</param>
/// <returns>The <see cref="bool"/> value indicating whether the logging level is enabled.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public bool IsEnabled(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Critical:
return log.IsFatalEnabled;
case LogLevel.Debug:
case LogLevel.Trace:
return log.IsDebugEnabled;
case LogLevel.Error:
return log.IsErrorEnabled;
case LogLevel.Information:
return log.IsInfoEnabled;
case LogLevel.Warning:
return log.IsWarnEnabled;
default:
throw new ArgumentOutOfRangeException(nameof(logLevel));
}
}
/// <summary>
/// Logs an exception into the log.
/// </summary>
/// <param name="logLevel">The log level.</param>
/// <param name="eventId">The event Id.</param>
/// <param name="state">The state.</param>
/// <param name="exception">The exception.</param>
/// <param name="formatter">The formatter.</param>
/// <typeparam name="TState">The type of the state.</typeparam>
/// <exception cref="ArgumentNullException">Throws when the <paramref name="formatter"/> is null.</exception>
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
if (!this.IsEnabled(logLevel))
{
return;
}
if (null == formatter)
{
throw new ArgumentNullException(nameof(formatter));
}
string message = null;
if (null != formatter)
{
message = formatter(state, exception);
}
if (null != exception && null != this.exceptionDetailsFormatter)
{
message = this.exceptionDetailsFormatter(message, exception);
}
if (!string.IsNullOrEmpty(message)
|| exception != null)
{
switch (logLevel)
{
case LogLevel.Critical:
log.Fatal(message);
break;
case LogLevel.Debug:
case LogLevel.Trace:
log.Debug(message);
break;
case LogLevel.Error:
log.Error(message);
break;
case LogLevel.Information:
log.Info(message);
break;
case LogLevel.Warning:
log.Warn(message);
break;
default:
log.Warn($"Encountered unknown log level {logLevel}, writing out as Info.");
log.Info(message, exception);
break;
}
}
}
/// <summary>
/// Defines custom formatter for logging exceptions.
/// </summary>
/// <param name="formatter">The formatting function to be used when formatting exceptions.</param>
/// <returns>The logger itself for fluent use.</returns>
public Log4NetLogger UsingCustomExceptionFormatter(Func<object, Exception, string> formatter)
{
this.exceptionDetailsFormatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
return this;
}
}
}