微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EventListener 负载收集规范

如何解决EventListener 负载收集规范

我正在使用 EventListeners 并获取 EventSources。

对于事件计数器,the payload is assumed to be a single element at index 0

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
    if (eventData.EventName != "EventCounters" 
            || eventData.Payload.Count <= 0 
            || !(eventData.Payload[0] is IDictionary<string,object> data) 
            || !data.TryGetValue("CounterType",out var counterType) 
            || !data.TryGetValue("Name",out var name))
        return;
    
    var metricType = counterType.ToString();
    float metricValue = 0;

    if ("Sum".Equals(metricType) && data.TryGetValue("Increment",out var increment))
    {
        metricValue = Convert.ToSingle(increment);
    }
    else if ("Mean".Equals(metricType) && data.TryGetValue("Mean",out var mean))
    {
        metricValue = Convert.ToSingle(mean);
    }
    
    // do something with your metric here...
}

对于日志消息,the other elements 2 and 4 of the payload are used

class MyEventListener : EventListener 
{
    protected override void OnEventSourceCreated(EventSource eventSource) 
    {
        if (eventSource.Name == "Microsoft-Extensions-Logging") 
        {
            // initialize a string,string dictionary of arguments to pass to the EventSource.
            // Turn on loggers matching ApP* to information,everything else (*) is the default level (which is EventLevel.Error)
            var args = new Dictionary<string,string>() { { "FilterSpecs","ApP*:information;*" } };
            // Set the default level (verbosity) to Error,and only ask for the formatted messages in this case.
            EnableEvents(eventSource,EventLevel.Error,LoggingEventSource.Keywords.FormattedMessage,args);
        }
    }

    protected override void OnEventWritten(EventWrittenEventArgs eventData) 
    {
        // Look for the formatted message event,which has the following argument layout (as defined in the LoggingEventSource.
        // FormattedMessage(LogLevel Level,int FactoryID,string LoggerName,string EventId,string FormattedMessage);
        if (eventData.EventName == "FormattedMessage")
            Console.WriteLine("Logger {0}: {1}",eventData.Payload[2],eventData.Payload[4]);
    }
}

我的问题是否有任何技术规范定义了在何处使用什么?

我试图了解有效负载何时是单个元素,何时是多个元素,以及每个元素的索引。

Microsoft documentation 不清楚。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。