Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed EmptyDefaultDictionary from MappedDiagnosticsContext #4115

Merged
merged 1 commit into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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