Skip to content

Commit

Permalink
Removed EmptyDefaultDictionary from MappedDiagnosticsContext (#4115)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Sep 19, 2020
1 parent 56ff6f4 commit abf8d9e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
33 changes: 12 additions & 21 deletions src/NLog/MappedDiagnosticsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using System.Linq;

namespace NLog
{
using System;
using System.Collections.Generic;

using Config;
using Internal;
using NLog.Config;
using NLog.Internal;

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

private static readonly IDictionary<string, object> EmptyDefaultDictionary = new SortHelpers.ReadOnlySingleBucketDictionary<string, object>();

private sealed class ItemRemover : IDisposable
{
private readonly string _item;
Expand Down Expand Up @@ -78,11 +73,7 @@ public void Dispose()
/// <returns></returns>
private static IDictionary<string, object> GetThreadDictionary(bool create = true)
{
var dictionary = ThreadLocalStorageHelper.GetDataForSlot<Dictionary<string, object>>(DataSlot, create);
if (dictionary == null && !create)
return EmptyDefaultDictionary;

return dictionary;
return ThreadLocalStorageHelper.GetDataForSlot<Dictionary<string, object>>(DataSlot, create);
}

/// <summary>
Expand Down Expand Up @@ -159,12 +150,12 @@ public static string Get(string item, IFormatProvider formatProvider)
/// <returns>The value of <paramref name="item"/>, if defined; otherwise <c>null</c>.</returns>
public static object GetObject(string item)
{
object o;

if (!GetThreadDictionary(false).TryGetValue(item, out o))
o = null;

return o;
var dictionary = GetThreadDictionary(false);
if (dictionary != null && dictionary.TryGetValue(item, out var o))
{
return o;
}
return null;
}

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

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

/// <summary>
Expand All @@ -200,7 +191,7 @@ public static void Remove(string item)
/// </summary>
public static void Clear()
{
GetThreadDictionary(true).Clear();
GetThreadDictionary(false)?.Clear();
}
}
}
2 changes: 1 addition & 1 deletion src/NLog/NestedDiagnosticsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static object[] GetAllObjects()
/// <summary>
/// Resets the stack to the original count during <see cref="IDisposable.Dispose"/>.
/// </summary>
private class StackPopper : IDisposable
private sealed class StackPopper : IDisposable
{
private readonly Stack<object> _stack;
private readonly int _previousCount;
Expand Down

0 comments on commit abf8d9e

Please sign in to comment.