-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an event listener to log SDK events. (#7320)
- This PR adds an event listener to log SDK events. The event listener is guarded by the env var and otherwise should have no effect. - Also this PR fixes the devguide to specify .net 8. Local testing is in progress... ## Azure IoT Edge PR checklist:
- Loading branch information
1 parent
a8d0494
commit 3557a22
Showing
6 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Core/ConsoleEventListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
//---------------------- | ||
// <auto-generated> | ||
// Used to trick stylecop into not processing this file | ||
// </auto-generated> | ||
//---------------------- | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Diagnostics.Tracing; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Azure.Devices.Logging | ||
{ | ||
/// <summary> | ||
/// Prints SDK events to Console output - the log level is set to TRACE | ||
/// NOTE: keep in sync with edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/ConsoleEventListener.cs | ||
/// </summary> | ||
public sealed class ConsoleEventListener : EventListener | ||
{ | ||
private readonly string[] _eventFilters; | ||
private readonly ILogger _logger; | ||
private readonly object _lock = new object(); | ||
|
||
public ConsoleEventListener(string filter, ILogger logger) | ||
{ | ||
_eventFilters = new string[1]; | ||
_eventFilters[0] = filter ?? throw new ArgumentNullException(nameof(filter)); | ||
_logger = logger; | ||
|
||
InitializeEventSources(); | ||
} | ||
|
||
public ConsoleEventListener(string[] filters, ILogger logger) | ||
{ | ||
_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)); | ||
} | ||
} | ||
_logger = logger; | ||
|
||
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 !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; | ||
#if NET451 | ||
// net451 doesn't have EventName, so we'll settle for EventId | ||
eventIdent = eventData.EventId.ToString(CultureInfo.InvariantCulture); | ||
#else | ||
eventIdent = eventData.EventName; | ||
#endif | ||
string text = $"{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture)} [SDK] [{eventData.EventSource.Name}-{eventIdent}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}"; | ||
_logger.LogInformation(text); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
edge-hub/core/src/Microsoft.Azure.Devices.Edge.Hub.Core/ConsoleEventListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
//---------------------- | ||
// <auto-generated> | ||
// Used to trick stylecop into not processing this file | ||
// </auto-generated> | ||
//---------------------- | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Diagnostics.Tracing; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Azure.Devices.Logging | ||
{ | ||
/// <summary> | ||
/// Prints SDK events to Console output - the log level is set to TRACE | ||
/// NOTE: Keep in sync with edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Core/ConsoleEventListener.cs | ||
/// </summary> | ||
public sealed class ConsoleEventListener : EventListener | ||
{ | ||
private readonly string[] _eventFilters; | ||
private readonly ILogger _logger; | ||
private readonly object _lock = new object(); | ||
|
||
public ConsoleEventListener(string filter, ILogger logger) | ||
{ | ||
_eventFilters = new string[1]; | ||
_eventFilters[0] = filter ?? throw new ArgumentNullException(nameof(filter)); | ||
_logger = logger; | ||
|
||
InitializeEventSources(); | ||
} | ||
|
||
public ConsoleEventListener(string[] filters, ILogger logger) | ||
{ | ||
_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)); | ||
} | ||
} | ||
_logger = logger; | ||
|
||
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 !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; | ||
#if NET451 | ||
// net451 doesn't have EventName, so we'll settle for EventId | ||
eventIdent = eventData.EventId.ToString(CultureInfo.InvariantCulture); | ||
#else | ||
eventIdent = eventData.EventName; | ||
#endif | ||
string text = $"{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture)} [SDK] [{eventData.EventSource.Name}-{eventIdent}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}"; | ||
_logger.LogInformation(text); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters