From c4b8097a3ce3b6c28673a638bfcc6c68402c9e90 Mon Sep 17 00:00:00 2001 From: Sindhu Nagesh Date: Wed, 10 Mar 2021 17:52:07 -0800 Subject: [PATCH] fix(e2e) - Change event logging to opt in to specific events only. (#1824) --- e2e/test/E2EMsTestBase.cs | 3 +- e2e/test/Helpers/ConsoleEventListener.cs | 78 +++++++----------------- 2 files changed, 23 insertions(+), 58 deletions(-) diff --git a/e2e/test/E2EMsTestBase.cs b/e2e/test/E2EMsTestBase.cs index cc8ed9cb28..9a06aef960 100644 --- a/e2e/test/E2EMsTestBase.cs +++ b/e2e/test/E2EMsTestBase.cs @@ -23,7 +23,6 @@ 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 @@ -41,7 +40,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(s_eventProviders); + _listener = new ConsoleEventListener(); } [TestCleanup] diff --git a/e2e/test/Helpers/ConsoleEventListener.cs b/e2e/test/Helpers/ConsoleEventListener.cs index 57dddccf4f..0ec251a538 100644 --- a/e2e/test/Helpers/ConsoleEventListener.cs +++ b/e2e/test/Helpers/ConsoleEventListener.cs @@ -1,6 +1,7 @@ // 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; @@ -8,82 +9,47 @@ namespace System.Diagnostics.Tracing { public sealed class ConsoleEventListener : EventListener { - private readonly string[] _eventFilters; - private readonly object _lock = new object(); - - 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)); - } - } + // 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" }; - InitializeEventSources(); - } - - private void InitializeEventSources() - { - foreach (EventSource source in EventSource.GetSources()) - { - EnableEvents(source, EventLevel.LogAlways); - } - } + private readonly object _lock = new object(); protected override void OnEventSourceCreated(EventSource eventSource) { - base.OnEventSourceCreated(eventSource); - EnableEvents( - eventSource, - EventLevel.LogAlways + if (_eventFilters.Any(filter => eventSource.Name.StartsWith(filter, StringComparison.OrdinalIgnoreCase))) + { + base.OnEventSourceCreated(eventSource); + EnableEvents( + eventSource, + EventLevel.LogAlways #if !NET451 , EventKeywords.All #endif ); + } } protected override void OnEventWritten(EventWrittenEventArgs eventData) { - if (_eventFilters == null) - { - return; - } - lock (_lock) { - if (_eventFilters.Any(ef => eventData.EventSource.Name.StartsWith(ef, StringComparison.Ordinal))) - { - string eventIdent; + 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; } } }