Skip to content

Commit

Permalink
fix(e2e) - Change event logging to opt in to specific events only.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinagesh committed Mar 11, 2021
1 parent d374837 commit 6ed381f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 58 deletions.
3 changes: 1 addition & 2 deletions e2e/test/E2EMsTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace Microsoft.Azure.Devices.E2ETests
/// </summary>
public class E2EMsTestBase : IDisposable
{
private static readonly string[] s_eventProviders = new string[] { "DotNetty-Default", "Microsoft-Azure-", };
private ConsoleEventListener _listener;

// Test specific logger instance
Expand All @@ -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]
Expand Down
73 changes: 17 additions & 56 deletions e2e/test/Helpers/ConsoleEventListener.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,50 @@
// 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;

namespace System.Diagnostics.Tracing
{
public sealed class ConsoleEventListener : EventListener
{
private readonly string[] _eventFilters;
private static readonly string[] _eventFilters = new string[] { "DotNetty-Default", "Microsoft-Azure-Devices" };
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));
}
}

InitializeEventSources();
}

private void InitializeEventSources()
{
foreach (EventSource source in EventSource.GetSources())
{
EnableEvents(source, EventLevel.LogAlways);
}
}

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;
}
}
}
Expand Down

0 comments on commit 6ed381f

Please sign in to comment.