Skip to content

Commit abf8d9e

Browse files
authored
Removed EmptyDefaultDictionary from MappedDiagnosticsContext (#4115)
1 parent 56ff6f4 commit abf8d9e

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

src/NLog/MappedDiagnosticsContext.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@
3131
// THE POSSIBILITY OF SUCH DAMAGE.
3232
//
3333

34-
using System.Linq;
35-
3634
namespace NLog
3735
{
3836
using System;
3937
using System.Collections.Generic;
40-
41-
using Config;
42-
using Internal;
38+
using NLog.Config;
39+
using NLog.Internal;
4340

4441
/// <summary>
4542
/// Mapped Diagnostics Context - a thread-local structure that keeps a dictionary
@@ -49,8 +46,6 @@ public static class MappedDiagnosticsContext
4946
{
5047
private static readonly object DataSlot = ThreadLocalStorageHelper.AllocateDataSlot();
5148

52-
private static readonly IDictionary<string, object> EmptyDefaultDictionary = new SortHelpers.ReadOnlySingleBucketDictionary<string, object>();
53-
5449
private sealed class ItemRemover : IDisposable
5550
{
5651
private readonly string _item;
@@ -78,11 +73,7 @@ public void Dispose()
7873
/// <returns></returns>
7974
private static IDictionary<string, object> GetThreadDictionary(bool create = true)
8075
{
81-
var dictionary = ThreadLocalStorageHelper.GetDataForSlot<Dictionary<string, object>>(DataSlot, create);
82-
if (dictionary == null && !create)
83-
return EmptyDefaultDictionary;
84-
85-
return dictionary;
76+
return ThreadLocalStorageHelper.GetDataForSlot<Dictionary<string, object>>(DataSlot, create);
8677
}
8778

8879
/// <summary>
@@ -159,12 +150,12 @@ public static string Get(string item, IFormatProvider formatProvider)
159150
/// <returns>The value of <paramref name="item"/>, if defined; otherwise <c>null</c>.</returns>
160151
public static object GetObject(string item)
161152
{
162-
object o;
163-
164-
if (!GetThreadDictionary(false).TryGetValue(item, out o))
165-
o = null;
166-
167-
return o;
153+
var dictionary = GetThreadDictionary(false);
154+
if (dictionary != null && dictionary.TryGetValue(item, out var o))
155+
{
156+
return o;
157+
}
158+
return null;
168159
}
169160

170161
/// <summary>
@@ -173,7 +164,7 @@ public static object GetObject(string item)
173164
/// <returns>A set of the names of all items in current thread-MDC.</returns>
174165
public static ICollection<string> GetNames()
175166
{
176-
return GetThreadDictionary(false).Keys;
167+
return GetThreadDictionary(false)?.Keys ?? ArrayHelper.Empty<string>();
177168
}
178169

179170
/// <summary>
@@ -183,7 +174,7 @@ public static ICollection<string> GetNames()
183174
/// <returns>A boolean indicating whether the specified <paramref name="item"/> exists in current thread MDC.</returns>
184175
public static bool Contains(string item)
185176
{
186-
return GetThreadDictionary(false).ContainsKey(item);
177+
return GetThreadDictionary(false)?.ContainsKey(item) == true;
187178
}
188179

189180
/// <summary>
@@ -200,7 +191,7 @@ public static void Remove(string item)
200191
/// </summary>
201192
public static void Clear()
202193
{
203-
GetThreadDictionary(true).Clear();
194+
GetThreadDictionary(false)?.Clear();
204195
}
205196
}
206197
}

src/NLog/NestedDiagnosticsContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static object[] GetAllObjects()
174174
/// <summary>
175175
/// Resets the stack to the original count during <see cref="IDisposable.Dispose"/>.
176176
/// </summary>
177-
private class StackPopper : IDisposable
177+
private sealed class StackPopper : IDisposable
178178
{
179179
private readonly Stack<object> _stack;
180180
private readonly int _previousCount;

0 commit comments

Comments
 (0)