forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add eventsource collector and console logger implementation (Azure#7577)
- Loading branch information
Showing
11 changed files
with
685 additions
and
489 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
Large diffs are not rendered by default.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
sdk/core/Azure.Core/src/Diagnostics/AzureEventSourceListener.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,67 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Tracing; | ||
using System.Globalization; | ||
using Azure.Core.Shared; | ||
|
||
namespace Azure.Core.Diagnostics | ||
{ | ||
public class AzureEventSourceListener: EventListener | ||
{ | ||
public const string TraitName = "AzureEventSource"; | ||
public const string TraitValue = "true"; | ||
|
||
private readonly List<EventSource> _eventSources = new List<EventSource>(); | ||
|
||
private readonly Action<EventWrittenEventArgs, string> _log; | ||
private readonly EventLevel _level; | ||
|
||
public AzureEventSourceListener(Action<EventWrittenEventArgs, string> log, EventLevel level) | ||
{ | ||
_log = log; | ||
_level = level; | ||
|
||
foreach (EventSource eventSource in _eventSources) | ||
{ | ||
OnEventSourceCreated(eventSource); | ||
} | ||
|
||
_eventSources.Clear(); | ||
} | ||
|
||
protected sealed override void OnEventSourceCreated(EventSource eventSource) | ||
{ | ||
base.OnEventSourceCreated(eventSource); | ||
|
||
if (_log == null) | ||
{ | ||
_eventSources.Add(eventSource); | ||
} | ||
|
||
if (eventSource.GetTrait(TraitName) == TraitValue) | ||
{ | ||
EnableEvents(eventSource, _level); | ||
} | ||
} | ||
|
||
protected sealed override void OnEventWritten(EventWrittenEventArgs eventData) | ||
{ | ||
_log(eventData, EventSourceEventFormatting.Format(eventData)); | ||
} | ||
|
||
public static AzureEventSourceListener CreateConsoleLogger(EventLevel level = EventLevel.Informational) | ||
{ | ||
return new AzureEventSourceListener((eventData, text) => Console.WriteLine("[{1}] {0}: {2}", eventData.EventSource.Name, eventData.Level, text), level); | ||
} | ||
|
||
public static AzureEventSourceListener CreateTraceLogger(EventLevel level = EventLevel.Informational) | ||
{ | ||
return new AzureEventSourceListener( | ||
(eventData, text) => Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "[{0}] {1}", eventData.Level, text), eventData.EventSource.Name), level); | ||
} | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
sdk/core/Azure.Core/src/Shared/EventSourceEventFormatting.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,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Diagnostics.Tracing; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Azure.Core.Shared | ||
{ | ||
internal class EventSourceEventFormatting | ||
{ | ||
public static string Format(EventWrittenEventArgs eventData) | ||
{ | ||
var payloadArray = eventData.Payload.ToArray(); | ||
|
||
ProcessPayloadArray(payloadArray); | ||
|
||
if (eventData.Message != null) | ||
{ | ||
return string.Format(CultureInfo.InvariantCulture, eventData.Message, payloadArray); | ||
} | ||
|
||
var stringBuilder = new StringBuilder(); | ||
stringBuilder.Append(eventData.EventName); | ||
|
||
for (int i = 0; i < eventData.PayloadNames.Count; i++) | ||
{ | ||
stringBuilder.AppendLine(); | ||
stringBuilder.Append(eventData.PayloadNames[i]).Append(" = ").Append(payloadArray[i]); | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
|
||
private static void ProcessPayloadArray(object[] payloadArray) | ||
{ | ||
for (int i = 0; i < payloadArray.Length; i++) | ||
{ | ||
payloadArray[i] = FormatValue(payloadArray[i]); | ||
} | ||
} | ||
|
||
private static object FormatValue(object o) | ||
{ | ||
if (o is byte[] bytes) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
foreach (byte b in bytes) | ||
{ | ||
stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0:X2}", b); | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
|
||
return o; | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
sdk/core/Azure.Core/tests/AzureEventSourceListenerTests.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,99 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
using System.Linq; | ||
using Azure.Core.Diagnostics; | ||
using Azure.Core.Testing; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Core.Tests | ||
{ | ||
[NonParallelizable] | ||
public class AzureEventSourceListenerTests | ||
{ | ||
[Test] | ||
public void CallsCallbackWhenMessageIsLogged() | ||
{ | ||
var invocations = new List<(EventWrittenEventArgs, string)>(); | ||
using var _ = new AzureEventSourceListener( | ||
(args, s) => | ||
{ | ||
invocations.Add((args, s)); | ||
}, EventLevel.Verbose); | ||
|
||
HttpPipelineEventSource.Singleton.Request(new MockRequest()); | ||
|
||
Assert.AreEqual(1, invocations.Count); | ||
var singleInvocation = invocations.Single(); | ||
|
||
Assert.AreEqual("Request", singleInvocation.Item1.EventName); | ||
Assert.NotNull(singleInvocation.Item2); | ||
} | ||
|
||
[Test] | ||
public void FormatsUsingMessageWhenAvailable() | ||
{ | ||
(EventWrittenEventArgs e, string message) = ExpectSingleEvent(() => TestSource.Log.LogWithMessage("a message")); | ||
Assert.AreEqual("Logging a message", message); | ||
} | ||
|
||
[Test] | ||
public void FormatsByteArrays() | ||
{ | ||
(EventWrittenEventArgs e, string message) = ExpectSingleEvent(() => TestSource.Log.LogWithByteArray(new byte[] { 0, 1, 233})); | ||
Assert.AreEqual("Logging 0001E9", message); | ||
} | ||
|
||
[Test] | ||
public void FormatsAsKeyValuesIfNoMessage() | ||
{ | ||
(EventWrittenEventArgs e, string message) = ExpectSingleEvent(() => TestSource.Log.LogWithoutMessage("a message", 5)); | ||
Assert.AreEqual("LogWithoutMessage" + Environment.NewLine + | ||
"message = a message" + Environment.NewLine + | ||
"other = 5", message); | ||
} | ||
|
||
private static (EventWrittenEventArgs, string) ExpectSingleEvent(Action logDelegate) | ||
{ | ||
var invocations = new List<(EventWrittenEventArgs, string)>(); | ||
using var _ = new AzureEventSourceListener( | ||
(args, s) => | ||
{ | ||
invocations.Add((args, s)); | ||
}, EventLevel.Verbose); | ||
logDelegate(); | ||
Assert.AreEqual(1, invocations.Count); | ||
return invocations.Single(); | ||
} | ||
|
||
private class TestSource : EventSource | ||
{ | ||
internal static TestSource Log { get; } = new TestSource(); | ||
|
||
private TestSource() : base("Test-source", EventSourceSettings.Default, "AzureEventSource", "true") | ||
{ | ||
} | ||
|
||
[Event(1, Message = "Logging {0}", Level = EventLevel.Critical)] | ||
public void LogWithMessage(string message) | ||
{ | ||
WriteEvent(1, message); | ||
} | ||
|
||
[Event(2, Level = EventLevel.Critical)] | ||
public void LogWithoutMessage(string message, int other) | ||
{ | ||
WriteEvent(2, message, other); | ||
} | ||
|
||
[Event(3, Message = "Logging {0}", Level = EventLevel.Critical)] | ||
public void LogWithByteArray(byte[] b) | ||
{ | ||
WriteEvent(3, b); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.