-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
MailDemonLogProvider.cs
65 lines (57 loc) · 1.89 KB
/
MailDemonLogProvider.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace MailDemon
{
public class MailDemonLogProvider : ILogger, ILoggerProvider
{
private NLog.Logger logger;
public void Dispose()
{
}
void ILogger.Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (logger != null)
{
string logText = formatter(state, exception);
/// WTF Microsoft, no way to turn these warnings off???
if (!logText.Contains("antiforgery", StringComparison.OrdinalIgnoreCase))
{
var nlogLevel = MailDemonLog.GetNLogLevel(logLevel);
string stackTrace = (exception == null ? string.Empty : exception.ToString());
logger.Log(nlogLevel, logText + (Environment.NewLine + stackTrace).Trim());
}
}
}
bool ILogger.IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel)
{
if (logger == null)
{
return false;
}
var nlogLevel = MailDemonLog.GetNLogLevel(logLevel);
return logger.IsEnabled(nlogLevel);
}
IDisposable ILogger.BeginScope<TState>(TState state)
{
return null;
}
ILogger ILoggerProvider.CreateLogger(string categoryName)
{
try
{
MailDemonLogProvider logger = new MailDemonLogProvider
{
logger = NLog.LogManager.GetCurrentClassLogger()
};
return logger;
}
catch
{
return null;
}
}
}
}