-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathLeafDeviceSdkLogger.cs
More file actions
69 lines (61 loc) · 2.24 KB
/
LeafDeviceSdkLogger.cs
File metadata and controls
69 lines (61 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Test.Common
{
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Linq;
using Microsoft.Extensions.Logging;
using Serilog;
/// <summary>
/// Prints SDK events to Console output - the log level is set to INFORMATION
/// </summary>
public sealed class LeafDeviceSdkLogger : EventListener
{
private readonly string[] eventFilters;
private readonly object @lock = new object();
public LeafDeviceSdkLogger(string filter)
: this(new string[] { filter })
{
}
public LeafDeviceSdkLogger(string[] filters)
{
this.eventFilters = filters ?? throw new ArgumentNullException(nameof(filters));
if (this.eventFilters.Length == 0)
{
throw new ArgumentException("Filters cannot be empty", nameof(filters));
}
foreach (string filter in this.eventFilters)
{
if (string.IsNullOrWhiteSpace(filter))
{
throw new ArgumentNullException(nameof(filters));
}
}
foreach (EventSource source in EventSource.GetSources())
{
this.EnableEvents(source, EventLevel.LogAlways);
}
}
protected override void OnEventSourceCreated(EventSource eventSource)
{
base.OnEventSourceCreated(eventSource);
this.EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All);
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (this.eventFilters == null)
{
return;
}
lock (this.@lock)
{
if (this.eventFilters.Any(ef => eventData.EventSource.Name.StartsWith(ef, StringComparison.Ordinal)))
{
string text = $"[{eventData.EventSource.Name}:{eventData.EventName}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : string.Empty)}";
Log.Verbose(text);
}
}
}
}
}