我在.NET 4.5 System.Diagnostic.Tracing.EventSource上遇到了一些麻烦。您可以在这篇文章的末尾找到完整的EventSource实现。
当我为这个EventSource创建一个侦听器时,它永远不会接收到一个事件。如有任何建议或问题,将不胜感激。
Microsoft.Practices.EnterpriseLibrary.SemanticLogging.ObservableLog edit1:-试过->不去。-将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);
}
}}
发布于 2014-05-27 09:42:09
事实上,我花了很多时间在这个问题上,所以我会在这里为其他可能遇到同样情况的人发布解决方案。
当将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;
}
}https://stackoverflow.com/questions/23865333
复制相似问题