Skip to content

Commit 9a0600a

Browse files
committed
Greatly simplify new IInternalLogger2 and use extension methods.
1 parent b1ba3de commit 9a0600a

File tree

2 files changed

+227
-266
lines changed

2 files changed

+227
-266
lines changed
Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Logging.Internal;
35

46
namespace NHibernate.Example.Web.Infrastructure
57
{
@@ -12,85 +14,35 @@ public NHibernateToMicrosoftLogger(ILogger msLogger)
1214
_msLogger = msLogger ?? throw new ArgumentNullException(nameof(msLogger));
1315
}
1416

15-
public void Fatal(Exception exception, string format, params object[] args)
17+
private static readonly Dictionary<InternalLogLevel, LogLevel> MapLevels = new Dictionary<InternalLogLevel, LogLevel>
1618
{
17-
_msLogger.LogCritical(exception, format, args);
18-
}
19-
20-
public void Fatal(string format, params object[] args)
21-
{
22-
_msLogger.LogCritical(format, args);
23-
}
24-
25-
public void Error(Exception exception, string format, params object[] args)
26-
{
27-
_msLogger.LogError(exception, format, args);
28-
}
29-
30-
public void Error(string format, params object[] args)
31-
{
32-
_msLogger.LogError(format, args);
33-
}
34-
35-
public void Warn(Exception exception, string format, params object[] args)
36-
{
37-
_msLogger.LogWarning(exception, format, args);
38-
}
39-
40-
public void Warn(string format, params object[] args)
41-
{
42-
_msLogger.LogWarning(format, args);
43-
}
44-
45-
public void Info(Exception exception, string format, params object[] args)
46-
{
47-
_msLogger.LogInformation(exception, format, args);
48-
}
49-
50-
public void Info(string format, params object[] args)
51-
{
52-
_msLogger.LogInformation(format, args);
53-
}
19+
{ InternalLogLevel.Trace, LogLevel.Trace },
20+
{ InternalLogLevel.Debug, LogLevel.Debug },
21+
{ InternalLogLevel.Warn, LogLevel.Warning },
22+
{ InternalLogLevel.Error, LogLevel.Error },
23+
{ InternalLogLevel.Fatal, LogLevel.Critical },
24+
{ InternalLogLevel.None, LogLevel.None },
25+
};
5426

55-
public void Debug(Exception exception, string format, params object[] args)
56-
{
57-
_msLogger.LogDebug(exception, format, args);
58-
}
59-
60-
public void Debug(string format, params object[] args)
61-
{
62-
_msLogger.LogDebug(format, args);
63-
}
64-
65-
public void Fatal(string message, Exception ex)
66-
{
67-
throw new NotImplementedException();
68-
}
69-
70-
public void Error(string message, Exception ex)
71-
{
72-
throw new NotImplementedException();
73-
}
27+
public bool IsDebugEnabled => _msLogger.IsEnabled(LogLevel.Debug);
28+
public bool IsInfoEnabled => _msLogger.IsEnabled(LogLevel.Information);
29+
public bool IsWarnEnabled => _msLogger.IsEnabled(LogLevel.Warning);
30+
public bool IsErrorEnabled => _msLogger.IsEnabled(LogLevel.Error);
31+
public bool IsFatalEnabled => _msLogger.IsEnabled(LogLevel.Critical);
7432

75-
public void Warn(string message, Exception ex)
33+
public void Log(InternalLogLevel logLevel, InternalLogValues state, Exception exception)
7634
{
77-
throw new NotImplementedException();
35+
_msLogger.Log(MapLevels[logLevel], 0, new FormattedLogValues(state.Format, state.Args), exception, MessageFormatter);
7836
}
7937

80-
public void Info(string message, Exception ex)
38+
public bool IsEnabled(InternalLogLevel logLevel)
8139
{
82-
throw new NotImplementedException();
40+
return _msLogger.IsEnabled(MapLevels[logLevel]);
8341
}
8442

85-
public void Debug(string message, Exception ex)
43+
private static string MessageFormatter(object state, Exception error)
8644
{
87-
throw new NotImplementedException();
45+
return state.ToString();
8846
}
89-
90-
public bool IsErrorEnabled => _msLogger.IsEnabled(LogLevel.Error);
91-
public bool IsFatalEnabled => _msLogger.IsEnabled(LogLevel.Critical);
92-
public bool IsDebugEnabled => _msLogger.IsEnabled(LogLevel.Debug);
93-
public bool IsInfoEnabled => _msLogger.IsEnabled(LogLevel.Information);
94-
public bool IsWarnEnabled => _msLogger.IsEnabled(LogLevel.Warning);
9547
}
9648
}

0 commit comments

Comments
 (0)