Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 2b2e0c6

Browse files
authored
Cleanup EventSource logging provider (#505)
1 parent bff2ae0 commit 2b2e0c6

File tree

8 files changed

+27
-26
lines changed

8 files changed

+27
-26
lines changed

src/Microsoft.Extensions.Logging.EventSource/EventSourceLogger.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Threading;
99
using Newtonsoft.Json;
1010

11-
namespace Microsoft.Extensions.Logging.EventSourceLogger
11+
namespace Microsoft.Extensions.Logging.EventSource
1212
{
1313
/// <summary>
1414
/// A logger that writes messages to EventSource instance.
@@ -19,9 +19,9 @@ namespace Microsoft.Extensions.Logging.EventSourceLogger
1919
/// </remarks>
2020
internal class EventSourceLogger : ILogger
2121
{
22+
private static int _activityIds;
2223
private readonly LoggingEventSource _eventSource;
2324
private readonly int _factoryID;
24-
private static int s_activityIds;
2525

2626
public EventSourceLogger(string categoryName, int factoryID, LoggingEventSource eventSource, EventSourceLogger next)
2727
{
@@ -131,7 +131,7 @@ public IDisposable BeginScope<TState>(TState state)
131131
return NoopDisposable.Instance;
132132
}
133133

134-
var id = Interlocked.Increment(ref s_activityIds);
134+
var id = Interlocked.Increment(ref _activityIds);
135135

136136
// If JsonMessage is on, use JSON format
137137
if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.JsonMessage))
@@ -188,7 +188,7 @@ public void Dispose()
188188

189189
private class NoopDisposable : IDisposable
190190
{
191-
public static NoopDisposable Instance = new NoopDisposable();
191+
public static readonly NoopDisposable Instance = new NoopDisposable();
192192

193193
public void Dispose()
194194
{

src/Microsoft.Extensions.Logging.EventSource/EventSourceLoggerFactoryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.Extensions.Logging.EventSourceLogger;
5+
using Microsoft.Extensions.Logging.EventSource;
66

77
namespace Microsoft.Extensions.Logging
88
{

src/Microsoft.Extensions.Logging.EventSource/EventSourceLoggerProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Diagnostics.Tracing;
66

7-
namespace Microsoft.Extensions.Logging.EventSourceLogger
7+
namespace Microsoft.Extensions.Logging.EventSource
88
{
99
/// <summary>
1010
/// The provider for the <see cref="EventSourceLogger"/>.
@@ -32,9 +32,7 @@ public EventSourceLoggerProvider(LoggingEventSource eventSource, EventSourceLogg
3232

3333
public EventSourceLoggerProvider Next { get; }
3434

35-
/// <summary>
3635
/// <inheritdoc />
37-
/// </summary>
3836
public ILogger CreateLogger(string categoryName)
3937
{
4038
// need to check if the filter spec and internal event source level has changed
@@ -51,7 +49,7 @@ public void Dispose()
5149
}
5250

5351
// Sets the filtering for a particular logger provider
54-
public void SetFilterSpec(string filterSpec)
52+
internal void SetFilterSpec(string filterSpec)
5553
{
5654
_filterSpec = filterSpec;
5755
_defaultLevel = GetDefaultLevel();

src/Microsoft.Extensions.Logging.EventSource/ExceptionInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
namespace Microsoft.Extensions.Logging.EventSourceLogger
4+
namespace Microsoft.Extensions.Logging.EventSource
55
{
66
/// <summary>
77
/// Represents information about exceptions that is captured by EventSourceLogger
@@ -14,6 +14,6 @@ internal class ExceptionInfo
1414
public string TypeName { get; set; }
1515
public string Message { get; set; }
1616
public int HResult { get; set; }
17-
public string VerboseMessage { get; set; } // This is the ToString() of the Exception
17+
public string VerboseMessage { get; set; } // This is the ToString() of the Exception
1818
}
1919
}

src/Microsoft.Extensions.Logging.EventSource/LoggingEventSource.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3-
43
using System.Collections.Generic;
54
using System.Diagnostics.Tracing;
65

7-
namespace Microsoft.Extensions.Logging.EventSourceLogger
6+
namespace Microsoft.Extensions.Logging.EventSource
87
{
98
/// <summary>
109
/// The LoggingEventSource is the bridge form all ILogger based logging to EventSource/EventListener logging.
@@ -70,7 +69,7 @@ namespace Microsoft.Extensions.Logging.EventSourceLogger
7069
/// }
7170
/// </summary>
7271
[EventSource(Name = "Microsoft-Extensions-Logging")]
73-
public class LoggingEventSource : EventSource
72+
internal class LoggingEventSource : System.Diagnostics.Tracing.EventSource
7473
{
7574
/// <summary>
7675
/// This is public from an EventSource consumer point of view, but since these defintions
@@ -103,14 +102,14 @@ public class Keywords
103102

104103
internal static readonly LogLevel LoggingDisabled = LogLevel.None + 1;
105104

105+
private readonly object _providerLock = new object();
106106
private string _filterSpec;
107107
private EventSourceLoggerProvider _loggingProviders;
108-
private object _lockObj = new object();
109108
private bool _checkLevel;
110109

111110
internal EventSourceLoggerProvider CreateLoggerProvider()
112111
{
113-
lock (_lockObj)
112+
lock (_providerLock)
114113
{
115114
var newLoggerProvider = new EventSourceLoggerProvider(this, _loggingProviders);
116115
_loggingProviders = newLoggerProvider;
@@ -197,24 +196,24 @@ internal void ActivityJsonStop(int ID, int FactoryID, string LoggerName)
197196
WriteEvent(7, ID, FactoryID, LoggerName);
198197
}
199198

200-
/// <summary>
201199
/// <inheritdoc />
202-
/// </summary>
203200
protected override void OnEventCommand(EventCommandEventArgs command)
204201
{
205-
lock (_lockObj)
202+
lock (_providerLock)
206203
{
207-
if ((command.Command == EventCommand.Update || command.Command == EventCommand.Enable))
204+
if (command.Command == EventCommand.Update || command.Command == EventCommand.Enable)
208205
{
209206
string filterSpec;
210207
if (!command.Arguments.TryGetValue("FilterSpecs", out filterSpec))
211-
filterSpec = ""; // This means turn on everything.
208+
{
209+
filterSpec = string.Empty; // This means turn on everything.
210+
}
212211

213212
SetFilterSpec(filterSpec);
214213
}
215214
else if (command.Command == EventCommand.Update || command.Command == EventCommand.Disable)
216215
{
217-
SetFilterSpec(null); // This means disable everything.
216+
SetFilterSpec(null); // This means disable everything.
218217
}
219218
}
220219
}
@@ -238,7 +237,7 @@ private void SetFilterSpec(string filterSpec)
238237
[NonEvent]
239238
internal void ApplyFilterSpec()
240239
{
241-
lock (_lockObj)
240+
lock (_providerLock)
242241
{
243242
if (_checkLevel)
244243
{

src/Microsoft.Extensions.Logging.EventSource/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
using System.Reflection;
55
using System.Resources;
6+
using System.Runtime.CompilerServices;
7+
8+
[assembly: InternalsVisibleTo("Microsoft.Extensions.Logging.EventSource.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
69

710
[assembly: AssemblyMetadata("Serviceable", "True")]
811
[assembly: NeutralResourcesLanguage("en-us")]

test/Microsoft.Extensions.Logging.EventSource.Test/EventSourceLoggerTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Diagnostics.Tracing;
77
using System.IO;
88
using System.Linq;
9-
using Microsoft.Extensions.Logging.EventSourceLogger;
9+
using Microsoft.Extensions.Logging.EventSource;
1010
using Newtonsoft.Json;
1111
using Xunit;
1212

@@ -421,7 +421,7 @@ public class ListenerSettings
421421
public string FilterSpec;
422422
}
423423

424-
private EventSource _loggingEventSource;
424+
private System.Diagnostics.Tracing.EventSource _loggingEventSource;
425425

426426
public TestEventListener()
427427
{
@@ -449,7 +449,7 @@ public void EnableEvents(ListenerSettings settings)
449449
EnableEvents(_loggingEventSource, settings.Level, settings.Keywords, args);
450450
}
451451

452-
protected override void OnEventSourceCreated(EventSource eventSource)
452+
protected override void OnEventSourceCreated(System.Diagnostics.Tracing.EventSource eventSource)
453453
{
454454
if (eventSource.Name == "Microsoft-Extensions-Logging")
455455
{

test/Microsoft.Extensions.Logging.EventSource.Test/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"buildOptions": {
3+
"keyFile": "../../tools/Key.snk",
34
"warningsAsErrors": true
45
},
56
"dependencies": {

0 commit comments

Comments
 (0)