forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
事件监听器EventListener使用研究,能够拦截框架内部动作,例如http请求等,不同版本框架的事件没有统一
- Loading branch information
Showing
3 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#if !NET4 | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
|
||
namespace NewLife.Log | ||
{ | ||
/// <summary>日志事件监听器。用于监听内置事件并写入日志</summary> | ||
public class LogEventListener : EventListener | ||
{ | ||
private readonly HashSet<String> _hash = new HashSet<String>(); | ||
private readonly HashSet<String> _hash2 = new HashSet<String>(); | ||
|
||
/// <summary>实例化</summary> | ||
/// <param name="sources"></param> | ||
public LogEventListener(String[] sources) | ||
{ | ||
foreach (var item in sources) | ||
{ | ||
_hash.Add(item); | ||
} | ||
} | ||
|
||
/// <summary>创建事件源。此时决定要不要跟踪</summary> | ||
/// <param name="eventSource"></param> | ||
protected override void OnEventSourceCreated(EventSource eventSource) | ||
{ | ||
if (_hash.Contains(eventSource.Name)) | ||
{ | ||
var log = XTrace.Log; | ||
|
||
var level = log.Level switch | ||
{ | ||
LogLevel.All => EventLevel.LogAlways, | ||
LogLevel.Debug => EventLevel.Verbose, | ||
LogLevel.Info => EventLevel.Informational, | ||
LogLevel.Warn => EventLevel.Warning, | ||
LogLevel.Error => EventLevel.Error, | ||
LogLevel.Fatal => EventLevel.Critical, | ||
LogLevel.Off => throw new NotImplementedException(), | ||
_ => EventLevel.Informational, | ||
}; | ||
|
||
EnableEvents(eventSource, level); | ||
} | ||
else if (!_hash2.Contains(eventSource.Name)) | ||
{ | ||
_hash2.Add(eventSource.Name); | ||
|
||
XTrace.WriteLine($"Source={eventSource.Name}"); | ||
} | ||
} | ||
|
||
/// <summary>写入事件。监听器拦截,并写入日志</summary> | ||
/// <param name="eventData"></param> | ||
protected override void OnEventWritten(EventWrittenEventArgs eventData) | ||
{ | ||
var log = XTrace.Log; | ||
|
||
var level = eventData.Level switch | ||
{ | ||
EventLevel.Informational => LogLevel.Info, | ||
EventLevel.LogAlways => LogLevel.All, | ||
EventLevel.Critical => LogLevel.Fatal, | ||
EventLevel.Error => LogLevel.Error, | ||
EventLevel.Warning => LogLevel.Warn, | ||
EventLevel.Verbose => LogLevel.Debug, | ||
_ => LogLevel.Info, | ||
}; | ||
|
||
#if NET45 | ||
XTrace.WriteLine($"#{eventData.EventSource?.Name} ID = {eventData.EventId}"); | ||
for (var i = 0; i < eventData.Payload.Count; i++) | ||
{ | ||
XTrace.WriteLine($"\tPayload = \"{eventData.Payload[i]}\""); | ||
} | ||
#elif NET50 | ||
XTrace.WriteLine($"#{eventData.EventSource?.Name} ThreadID = {eventData.OSThreadId} ID = {eventData.EventId} Name = {eventData.EventName}"); | ||
for (var i = 0; i < eventData.Payload.Count; i++) | ||
{ | ||
XTrace.WriteLine($"\tName = \"{eventData.PayloadNames[i]}\" Value = \"{eventData.Payload[i]}\""); | ||
} | ||
#else | ||
XTrace.WriteLine($"#{eventData.EventSource?.Name} ID = {eventData.EventId} Name = {eventData.EventName}"); | ||
for (var i = 0; i < eventData.Payload.Count; i++) | ||
{ | ||
XTrace.WriteLine($"\tName = \"{eventData.PayloadNames[i]}\" Value = \"{eventData.Payload[i]}\""); | ||
} | ||
#endif | ||
|
||
if (!eventData.Message.IsNullOrEmpty()) log.Write(level, eventData.Message); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters