From 11172aadd42e8000f1b5e37b2b09b83cb725d320 Mon Sep 17 00:00:00 2001 From: Abhipsa Misra Date: Fri, 19 Mar 2021 14:59:35 -0700 Subject: [PATCH] Revert "fix(e2e) - Change event logging to opt in to specific events only. (#1824)" This reverts commit c405f4f4d2906e26646974010723bcfa4dfa747f. --- e2e/test/E2EMsTestBase.cs | 3 +- e2e/test/Helpers/ConsoleEventListener.cs | 78 +++++++++++++++++------- 2 files changed, 58 insertions(+), 23 deletions(-) diff --git a/e2e/test/E2EMsTestBase.cs b/e2e/test/E2EMsTestBase.cs index 9a06aef960..cc8ed9cb28 100644 --- a/e2e/test/E2EMsTestBase.cs +++ b/e2e/test/E2EMsTestBase.cs @@ -23,6 +23,7 @@ namespace Microsoft.Azure.Devices.E2ETests /// public class E2EMsTestBase : IDisposable { + private static readonly string[] s_eventProviders = new string[] { "DotNetty-Default", "Microsoft-Azure-", }; private ConsoleEventListener _listener; // Test specific logger instance @@ -40,7 +41,7 @@ public void TestInitialize() // Note: Events take long and increase run time of the test suite, so only using trace. Logger.Trace($"Starting test - {TestContext.TestName}", SeverityLevel.Information); - _listener = new ConsoleEventListener(); + _listener = new ConsoleEventListener(s_eventProviders); } [TestCleanup] diff --git a/e2e/test/Helpers/ConsoleEventListener.cs b/e2e/test/Helpers/ConsoleEventListener.cs index 0ec251a538..57dddccf4f 100644 --- a/e2e/test/Helpers/ConsoleEventListener.cs +++ b/e2e/test/Helpers/ConsoleEventListener.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -9,47 +8,82 @@ namespace System.Diagnostics.Tracing { public sealed class ConsoleEventListener : EventListener { - // Configure this value to filter all the necessary events when OnEventSourceCreated is called. - // OnEventSourceCreated is triggered as soon as the EventListener is registered and an event source is created. - // So trying to configure this value in the ConsoleEventListener constructor does not work. - // The OnEventSourceCreated can be triggered sooner than the filter is initialized in the ConsoleEventListener constructor. - private static string[] _eventFilters = new string[] { "DotNetty-Default", "Microsoft-Azure-Devices" }; - + private readonly string[] _eventFilters; private readonly object _lock = new object(); - protected override void OnEventSourceCreated(EventSource eventSource) + public ConsoleEventListener(string filter) + { + _eventFilters = new string[1]; + _eventFilters[0] = filter ?? throw new ArgumentNullException(nameof(filter)); + + InitializeEventSources(); + } + + public ConsoleEventListener(string[] filters) + { + _eventFilters = filters ?? throw new ArgumentNullException(nameof(filters)); + if (_eventFilters.Length == 0) + { + throw new ArgumentException("Filters cannot be empty", nameof(filters)); + } + + foreach (string filter in _eventFilters) + { + if (string.IsNullOrWhiteSpace(filter)) + { + throw new ArgumentNullException(nameof(filters)); + } + } + + InitializeEventSources(); + } + + private void InitializeEventSources() { - if (_eventFilters.Any(filter => eventSource.Name.StartsWith(filter, StringComparison.OrdinalIgnoreCase))) + foreach (EventSource source in EventSource.GetSources()) { - base.OnEventSourceCreated(eventSource); - EnableEvents( - eventSource, - EventLevel.LogAlways + EnableEvents(source, EventLevel.LogAlways); + } + } + + protected override void OnEventSourceCreated(EventSource eventSource) + { + base.OnEventSourceCreated(eventSource); + EnableEvents( + eventSource, + EventLevel.LogAlways #if !NET451 , EventKeywords.All #endif ); - } } protected override void OnEventWritten(EventWrittenEventArgs eventData) { + if (_eventFilters == null) + { + return; + } + lock (_lock) { - string eventIdent; + if (_eventFilters.Any(ef => eventData.EventSource.Name.StartsWith(ef, StringComparison.Ordinal))) + { + string eventIdent; #if NET451 // net451 doesn't have EventName, so we'll settle for EventId eventIdent = eventData.EventId.ToString(CultureInfo.InvariantCulture); #else - eventIdent = eventData.EventName; + eventIdent = eventData.EventName; #endif - string text = $"{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture)} [{eventData.EventSource.Name}-{eventIdent}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}"; + string text = $"{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture)} [{eventData.EventSource.Name}-{eventIdent}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}"; - ConsoleColor origForeground = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(text); - Debug.WriteLine(text); - Console.ForegroundColor = origForeground; + ConsoleColor origForeground = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.DarkYellow; + Console.WriteLine(text); + Debug.WriteLine(text); + Console.ForegroundColor = origForeground; + } } } }