Created
February 2, 2017 12:21
-
-
Save itn3000/738c312b63fc382914de3084730431e6 to your computer and use it in GitHub Desktop.
example EventSource and EventListener
This file contains hidden or 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
| using System; | |
| using System.Diagnostics.Tracing; | |
| using System.Diagnostics; | |
| class Program | |
| { | |
| class MyEventListener : EventListener | |
| { | |
| protected override void OnEventWritten(EventWrittenEventArgs eventData) | |
| { | |
| Console.WriteLine($"{eventData.Channel},{eventData.EventName},{eventData.EventSource},{eventData.Message}"); | |
| for (int i = 0; i < eventData.Payload.Count; i++) | |
| { | |
| Console.WriteLine($"{eventData.Payload[i]},{eventData.PayloadNames[i]}"); | |
| } | |
| } | |
| } | |
| [EventSource(Name = "MyEventSource")] | |
| class MyEvent : EventSource | |
| { | |
| public static MyEvent Instance { get; } = new MyEvent(); | |
| private MyEvent() | |
| { | |
| } | |
| [Event(1, Message = "Log={0}")] | |
| public void Log(string message) | |
| { | |
| WriteEvent(1, message); | |
| } | |
| } | |
| static void Main(string[] args) | |
| { | |
| using (var listener = new MyEventListener()) | |
| { | |
| listener.EnableEvents(MyEvent.Instance, EventLevel.Informational); | |
| MyEvent.Instance.Log("mogemoge"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment