-
Notifications
You must be signed in to change notification settings - Fork 6
/
HSTraceListener.cs
95 lines (81 loc) · 2.9 KB
/
HSTraceListener.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
using System;
using System.Diagnostics;
using System.Globalization;
namespace Hspi
{
internal sealed class HSTraceListener : TraceListener
{
public HSTraceListener(ILogger logger)
{
loggerWeakReference = new WeakReference<ILogger>(logger);
}
public override bool IsThreadSafe => true;
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null))
{
return;
}
TraceEvent(eventCache, source, eventType, id, string.Format(CultureInfo.InvariantCulture, format, args));
}
public override void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string message)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null))
{
return;
}
if (loggerWeakReference.TryGetTarget(out var logger))
{
try
{
switch (eventType)
{
case TraceEventType.Critical:
case TraceEventType.Error:
logger.LogError(message);
break;
case TraceEventType.Warning:
logger.LogWarning(message);
break;
case TraceEventType.Information:
logger.LogInfo(message);
break;
case TraceEventType.Verbose:
logger.LogDebug(message);
break;
case TraceEventType.Start:
case TraceEventType.Stop:
case TraceEventType.Suspend:
case TraceEventType.Resume:
case TraceEventType.Transfer:
logger.LogInfo(message);
break;
}
}
catch
{ }
}
}
public override void Write(string message)
{
LogDebug(message);
}
public override void WriteLine(string message)
{
LogDebug(message);
}
private void LogDebug(string message)
{
if (loggerWeakReference.TryGetTarget(out var logger))
{
try
{
logger.LogDebug(message);
}
catch
{ }
}
}
private readonly WeakReference<ILogger> loggerWeakReference;
}
}