0

我在使用 .NET 4.5 System.Diagnostic.Tracing.EventSource 时遇到了一些问题。您可以在本文末尾找到完整的 EventSource 实现。

当我为此 EventSource 创建一个侦听器时,它永远不会收到事件。所有提示或问题将不胜感激。

编辑1:-尝试了Microsoft.Practices.EnterpriseLibrary.SemanticLogging.ObservableLog->不行。- 将 EventLevel 设置为详细的侦听器。- 我可以使用 PerfView.exe 捕获事件

我如何激活我的听众:

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

我的听众:

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

    }
}

事件来源:

 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);
    }
}

}

4

1 回答 1

4

好吧,实际上我已经花了很多时间在这上面,所以我会在这里为可能遇到相同问题的其他人发布解决方案。

当您将 EventListener 附加到使用自定义关键字(通过属性)的 EventSource 时,您必须指定要订阅的关键字。

如果要包含所有关键字,请将它们组合起来:

    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;
        }
    }
于 2014-05-27T09:42:09.943 回答