forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog4NetLogger.Core.cs
More file actions
98 lines (88 loc) · 3.22 KB
/
Log4NetLogger.Core.cs
File metadata and controls
98 lines (88 loc) · 3.22 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
#if NETSTANDARD2_0
using Microsoft.Extensions.Logging;
using System;
using System.Reflection;
namespace ServiceStack.Logging.Log4Net
{
public partial class Log4NetLogger : ILogger
{
private Assembly mainAssembly = Assembly.GetEntryAssembly();
public Log4NetLogger(string name)
{
log = log4net.LogManager.GetLogger(mainAssembly, name);
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
/// <summary>
/// Check is a logLevel is enabled.
/// </summary>
/// <param name="logLevel">the level.</param>
/// <returns></returns>
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 a message from Logging into Log4Net.
/// </summary>
/// <typeparam name="TState"></typeparam>
/// <param name="logLevel">The level.</param>
/// <param name="eventId">The eventId.</param>
/// <param name="state">The state.</param>
/// <param name="exception">Exception related to the event.</param>
/// <param name="formatter">The formatter.</param>
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
return;
if (formatter == null)
throw new ArgumentNullException(nameof(formatter));
var message = formatter(state, exception);
if (!string.IsNullOrEmpty(message) || exception != null)
{
switch (logLevel)
{
case LogLevel.Critical:
log.Fatal(message, exception);
break;
case LogLevel.Debug:
case LogLevel.Trace:
log.Debug(message, exception);
break;
case LogLevel.Error:
log.Error(message, exception);
break;
case LogLevel.Information:
log.Info(message, exception);
break;
case LogLevel.Warning:
log.Warn(message, exception);
break;
default:
log.Warn($"Encountered unknown log level {logLevel}, writing out as Info.");
log.Info(message, exception);
break;
}
}
}
}
}
#endif