简体   繁体   中英

System.Diagnostic.Tracing.EventSource - no events in EventListener

I am experiencing some trouble with the .NET 4.5 System.Diagnostic.Tracing.EventSource. You can find the complete EventSource implementation at the end of this post.

When I create a listener for this EventSource it never receives an event. All tips, or questions would be appreciated.

edit1: - tried the Microsoft.Practices.EnterpriseLibrary.SemanticLogging.ObservableLog -> no go. - set the EventLevel to verbose for listener. - I CAN capture events using PerfView.exe

How i activate my listener:

_sink = new SignalRListener();
_sink.EnableEvents(GatewayEvent.Log, EventLevel.Verbose);

My listener:

internal class SignalRListener : EventListener
{
    protected override void OnEventWritten(EventWrittenEventArgs eventData)
    {
        CommandAndControlHub.SentEventEntry(eventData);

    }
}

The event source:

 using System;
 //using Microsoft.Diagnostics.Tracing;
 using System.Diagnostics.Tracing;

namespace DDA.Gateway
{
[EventSource(Name = "DDA-Gateway")]
public sealed class GatewayEvent : EventSource
{
    public class Keywords
    {

        public const EventKeywords ServiceInvoked = (EventKeywords)1;
        public const EventKeywords Diagnostic = (EventKeywords)2;
        public const EventKeywords Perf = (EventKeywords)4;
    }

    public class Tasks
    {
        public const EventTask ProcessRequest = (EventTask)1;
        public const EventTask ConnectingToHub = (EventTask)2;
        public const EventTask QueryingDataInterface = (EventTask)4;
    }

    private readonly static Lazy<GatewayEvent> Instance = new Lazy<GatewayEvent>(() => new GatewayEvent());
    private GatewayEvent() 
    {
    }
    public static GatewayEvent Log { get { return Instance.Value; } }
    [Event(1001, Message = "Application Failure: {0}",
    Level = EventLevel.Critical, Keywords = Keywords.Diagnostic)]
    public void Failure(string message)
    {
        if (this.IsEnabled())
        {
            this.WriteEvent(1001, message);
        }
    }
    [Event(1002, Message = "Connecting to hub:{0}", Opcode = EventOpcode.Start,
        Task = Tasks.ConnectingToHub, Keywords = Keywords.Diagnostic | Keywords.Perf, 
        Level = EventLevel.Informational)]
    public void ConnectingToHubStart(string url)
    {
        if (this.IsEnabled())
        {
            this.WriteEvent(1002, url); 
        }
    }

    [Event(1003, Message = "Success:{0} - Elapsed time:{1}", Opcode = EventOpcode.Stop,
        Task = Tasks.ConnectingToHub, Keywords = Keywords.Diagnostic | Keywords.Perf,
        Level = EventLevel.Informational)]
    public void ConnectingToHubEnd(bool success, string elapsedTime)
    {
        if (this.IsEnabled())
        {
            this.WriteEvent(1003, success, elapsedTime); 
        }
    }
    [Event(1004, Message = "Data received:\r\n{0}", 
        Keywords=Keywords.Diagnostic | Keywords.Perf, Level=EventLevel.Verbose)]
    public void DataReceivedByHubClient(string data)
    {
        if (IsEnabled())
        {
            this.WriteEvent(1004, data); 
        }
    }
    [Event(1005, Message = "Hub client reports a slow connection.",
        Keywords = Keywords.Diagnostic | Keywords.Perf, Level = EventLevel.Warning)]
    public void ConnectionSlow()
    {
        if (IsEnabled())
        {
            this.WriteEvent(1005); 
        }
    }

    [Event(1006, Message = "Hub client reports an arror.\r\n{0}",
        Keywords = Keywords.Diagnostic | Keywords.Perf, Level = EventLevel.Warning)]
    public void HubClientEncounteredAnError(string exceptionDetails)
    {
        if (IsEnabled())
        {
            this.WriteEvent(1006, exceptionDetails); 
        }
    }

    [Event(1007, Message = "Start Processing Request {0} for: {1}.{2}", Opcode = EventOpcode.Start,
        Task = Tasks.ProcessRequest, Keywords = Keywords.Diagnostic | Keywords.Perf,
        Level = EventLevel.Verbose)]
    public void ProcessRequestStart(string reqId, string service, string method)
    {
        if (this.IsEnabled())
        {
            this.WriteEvent(1007, reqId, service, method);
        }
    }

    [Event(1008, Message = "Ended Request process. Elapsed time:", Opcode = EventOpcode.Stop,
        Task = Tasks.ProcessRequest, Keywords = Keywords.Diagnostic | Keywords.Perf,
        Level = EventLevel.Verbose)]
    public void ProcessRequestEnd(string elapsedTime)
    {
        if (this.IsEnabled())
            this.WriteEvent(1008, elapsedTime);
    }

    [Event(1009, Message = "Request sent ({0})", Opcode = EventOpcode.Send,
        Task = Tasks.ProcessRequest, Keywords = Keywords.Diagnostic | Keywords.Perf,
        Level = EventLevel.Verbose)]
    public void ProcessRequestSendResponse(string sendDetails)
    {
        if (this.IsEnabled())
            this.WriteEvent(1009, sendDetails);
    }
}

}

Well actually i've spent a lot of time on this, so i'll post the solution here for other who may encounter the same.

When you attach an EventListener to an EventSource that uses custom keywords (via attributes) you must specify the keywords for which you wish to subscribe.

If you want to include all keywords, combine them:

    public class Keywords
    {

        public const EventKeywords ServiceInvoked = (EventKeywords)1;
        public const EventKeywords Diagnostic = (EventKeywords)2;
        public const EventKeywords Perf = (EventKeywords)4;

        public static EventKeywords GetAll()
        {
            return ServiceInvoked | Diagnostic  | Perf;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM