Skip to content

Commit

Permalink
Revert "fix(e2e) - Change event logging to opt in to specific events …
Browse files Browse the repository at this point in the history
…only. (#1824)"

This reverts commit c405f4f.
  • Loading branch information
abhipsaMisra authored Mar 19, 2021
1 parent 5ac2f43 commit 11172aa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
3 changes: 2 additions & 1 deletion e2e/test/E2EMsTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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 @@ -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]
Expand Down
78 changes: 56 additions & 22 deletions e2e/test/Helpers/ConsoleEventListener.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,89 @@
// 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
{
// 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;
}
}
}
}
Expand Down

0 comments on commit 11172aa

Please sign in to comment.