31
31
// THE POSSIBILITY OF SUCH DAMAGE.
32
32
//
33
33
34
- using System . Linq ;
35
-
36
34
namespace NLog
37
35
{
38
36
using System ;
39
37
using System . Collections . Generic ;
40
-
41
- using Config ;
42
- using Internal ;
38
+ using NLog . Config ;
39
+ using NLog . Internal ;
43
40
44
41
/// <summary>
45
42
/// Mapped Diagnostics Context - a thread-local structure that keeps a dictionary
@@ -49,8 +46,6 @@ public static class MappedDiagnosticsContext
49
46
{
50
47
private static readonly object DataSlot = ThreadLocalStorageHelper . AllocateDataSlot ( ) ;
51
48
52
- private static readonly IDictionary < string , object > EmptyDefaultDictionary = new SortHelpers . ReadOnlySingleBucketDictionary < string , object > ( ) ;
53
-
54
49
private sealed class ItemRemover : IDisposable
55
50
{
56
51
private readonly string _item ;
@@ -78,11 +73,7 @@ public void Dispose()
78
73
/// <returns></returns>
79
74
private static IDictionary < string , object > GetThreadDictionary ( bool create = true )
80
75
{
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 ) ;
86
77
}
87
78
88
79
/// <summary>
@@ -159,12 +150,12 @@ public static string Get(string item, IFormatProvider formatProvider)
159
150
/// <returns>The value of <paramref name="item"/>, if defined; otherwise <c>null</c>.</returns>
160
151
public static object GetObject ( string item )
161
152
{
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 ;
168
159
}
169
160
170
161
/// <summary>
@@ -173,7 +164,7 @@ public static object GetObject(string item)
173
164
/// <returns>A set of the names of all items in current thread-MDC.</returns>
174
165
public static ICollection < string > GetNames ( )
175
166
{
176
- return GetThreadDictionary ( false ) . Keys ;
167
+ return GetThreadDictionary ( false ) ? . Keys ?? ArrayHelper . Empty < string > ( ) ;
177
168
}
178
169
179
170
/// <summary>
@@ -183,7 +174,7 @@ public static ICollection<string> GetNames()
183
174
/// <returns>A boolean indicating whether the specified <paramref name="item"/> exists in current thread MDC.</returns>
184
175
public static bool Contains ( string item )
185
176
{
186
- return GetThreadDictionary ( false ) . ContainsKey ( item ) ;
177
+ return GetThreadDictionary ( false ) ? . ContainsKey ( item ) == true ;
187
178
}
188
179
189
180
/// <summary>
@@ -200,7 +191,7 @@ public static void Remove(string item)
200
191
/// </summary>
201
192
public static void Clear ( )
202
193
{
203
- GetThreadDictionary ( true ) . Clear ( ) ;
194
+ GetThreadDictionary ( false ) ? . Clear ( ) ;
204
195
}
205
196
}
206
197
}
0 commit comments