当前位置: 首页>>代码示例>>C#>>正文


C# ObservableEventListener.ThrottleEventsWithEventId方法代码示例

本文整理汇总了C#中ObservableEventListener.ThrottleEventsWithEventId方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableEventListener.ThrottleEventsWithEventId方法的具体用法?C# ObservableEventListener.ThrottleEventsWithEventId怎么用?C# ObservableEventListener.ThrottleEventsWithEventId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ObservableEventListener的用法示例。


在下文中一共展示了ObservableEventListener.ThrottleEventsWithEventId方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            var listener = new ObservableEventListener();
            listener.EnableEvents(RxFloodQuickStartEventSource.Log, EventLevel.LogAlways, Keywords.All);

            // ThrottleEventsWithEventId is a custom extension method that shows how you can leverage the power of Reactive Extensions (Rx) 
            // to perform filtering (or transformation) of the event stream before it is sent to the underlying sink.
            // In this case, ThrottleEventsWithEventId will throttle entries with EventID=4 and mute additional occurrences for 15 seconds.
            // This prevents a particular event from flooding the log sink, making it difficult to diagnose other issues.
            // This can be useful in the case that a high-throughput event does not have a keyword or verbosity setting that makes it easy
            // to exclude it in the call to listener.EnableEvents(EventSource, EventLevel, EventKeywords).

            // Note: For basic scenarios without this extra filtering, you DO NOT need to use Rx, and SLAB does not depend on it.

            var subscription = listener
                                .ThrottleEventsWithEventId(TimeSpan.FromSeconds(15), ThrottledEventId)
                                .LogToConsole(SingleLineFormatter);

            // The previous custom extension method (ThrottleEventsWithEventId) is all that is needed to call to throttle
            // an event that is flooding the log. 
            // The rest of the code in this QuickStart is here to show an interactive demo of how it looks if this filter is turned on or off.
            bool currentlyThrottling = true;

            var cts = new CancellationTokenSource();

            Console.WriteLine("This program simulates the scenario of a particular event being logged multiple times in succession when a certain condition occurs,");
            Console.WriteLine("such as when there is a transient or expected connectivity error during system upgrades.");
            Console.WriteLine();
            Console.WriteLine("While the application is logging messages, use the following commands:");
            Console.WriteLine(" [ESC]      Exists the application.");
            Console.WriteLine(" [Spacebar] Toggles the throttling filter.");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Press any key to start doing background work.");

            var key = Console.ReadKey(false);
            if (key.Key == ConsoleKey.Escape)
            {
                return;
            }

            DoBackgroundWork(cts.Token);

            while (!cts.IsCancellationRequested)
            {
                key = Console.ReadKey(false);
                switch (key.Key)
                {
                    case ConsoleKey.Spacebar:
                        subscription.Dispose();
                        if (currentlyThrottling)
                        {
                            Console.WriteLine("Filter toggled: event entries will not be throttled. In this scenario, if there is no post-filtering of events, important messages could go unnoticed.");
                            Thread.Sleep(TimeSpan.FromSeconds(3));
                            currentlyThrottling = false;

                            // Note that the events are sent directly to the console, without using Reactive Extensions.
                            subscription = listener
                                                .LogToConsole(SingleLineFormatter);
                        }
                        else
                        {
                            Console.WriteLine("Filter toggled: event entries with ID {0} will be throttled for 15 seconds to prevent that type of entry to flood the log.", ThrottledEventId);
                            Thread.Sleep(TimeSpan.FromSeconds(3));
                            currentlyThrottling = true;

                            // Note that the events are filtered first and then sent to the console, using Reactive Extensions.
                            subscription = listener
                                                .ThrottleEventsWithEventId(TimeSpan.FromSeconds(15), ThrottledEventId)
                                                .LogToConsole(SingleLineFormatter);
                        }
                        
                        break;

                    case ConsoleKey.Escape:
                        cts.Cancel();
                        break;
                }
            }

            listener.Dispose();
        }
开发者ID:guyzo,项目名称:semantic-logging,代码行数:82,代码来源:Program.cs


注:本文中的ObservableEventListener.ThrottleEventsWithEventId方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。