Skip to content

Commit

Permalink
Follow up on EventSourceListener review comments (Azure#7611)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Sep 13, 2019
1 parent ac66dfb commit 58748cc
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion sdk/core/Azure.Core/src/Diagnostics/AzureEventSourceListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace Azure.Core.Diagnostics
{
/// <summary>
/// Implementation of <see cref="EventListener"/> that listens to events produces by Azure SDK Client libraries
/// </summary>
public class AzureEventSourceListener: EventListener
{
public const string TraitName = "AzureEventSource";
Expand All @@ -20,9 +23,15 @@ public class AzureEventSourceListener: EventListener
private readonly Action<EventWrittenEventArgs, string> _log;
private readonly EventLevel _level;

/// <summary>
/// Creates an instance of <see cref="AzureEventSourceListener"/> that executes a <paramref name="log"/> callback every time event is written.
/// </summary>
/// <param name="log">The <see cref="System.Action{EventWrittenEventArgs, String}"/> to call when event is written. The second parameter is formatted message.</param>
/// <param name="level">The level of events to enable.</param>
public AzureEventSourceListener(Action<EventWrittenEventArgs, string> log, EventLevel level)
{
_log = log;
_log = log ?? throw new ArgumentNullException(nameof(log));

_level = level;

foreach (EventSource eventSource in _eventSources)
Expand Down Expand Up @@ -53,11 +62,19 @@ protected sealed override void OnEventWritten(EventWrittenEventArgs eventData)
_log(eventData, EventSourceEventFormatting.Format(eventData));
}

/// <summary>
/// Creates a new instance of <see cref="AzureEventSourceListener"/> that forwards events to <see cref="Console.WriteLine(string)"/>
/// </summary>
/// <param name="level">The level of events to enable.</param>
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);
}

/// <summary>
/// Creates a new instance of <see cref="AzureEventSourceListener"/> that forwards events to <see cref="Trace.WriteLine(object)"/>
/// </summary>
/// <param name="level">The level of events to enable.</param>
public static AzureEventSourceListener CreateTraceLogger(EventLevel level = EventLevel.Informational)
{
return new AzureEventSourceListener(
Expand Down

0 comments on commit 58748cc

Please sign in to comment.