1
#region Copyright & License
3
// Copyright 2001-2005 The Apache Software Foundation
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
19
// .NET Compact Framework 1.0 has no support for System.Runtime.Remoting.Messaging.CallContext
23
using System.Collections;
25
using System.Runtime.Remoting.Messaging;
27
namespace log4net.Util
30
/// Implementation of Properties collection for the <see cref="log4net.LogicalThreadContext"/>
34
/// Class implements a collection of properties that is specific to each thread.
35
/// The class is not synchronized as each thread has its own <see cref="PropertiesDictionary"/>.
38
/// <author>Nicko Cadell</author>
39
public sealed class LogicalThreadContextProperties : ContextPropertiesBase
41
#region Public Instance Constructors
48
/// Initializes a new instance of the <see cref="LogicalThreadContextProperties" /> class.
51
internal LogicalThreadContextProperties()
55
#endregion Public Instance Constructors
57
#region Public Instance Properties
60
/// Gets or sets the value of a property
63
/// The value for the property with the specified key
67
/// Get or set the property value for the <paramref name="key"/> specified.
70
override public object this[string key]
74
// Don't create the dictionary if it does not already exist
75
PropertiesDictionary dictionary = GetProperties(false);
76
if (dictionary != null)
78
return dictionary[key];
84
// Force the dictionary to be created
85
GetProperties(true)[key] = value;
89
#endregion Public Instance Properties
91
#region Public Instance Methods
96
/// <param name="key">the key for the entry to remove</param>
99
/// Remove the value for the specified <paramref name="key"/> from the context.
102
public void Remove(string key)
104
PropertiesDictionary dictionary = GetProperties(false);
105
if (dictionary != null)
107
dictionary.Remove(key);
112
/// Clear all the context properties
116
/// Clear all the context properties
121
PropertiesDictionary dictionary = GetProperties(false);
122
if (dictionary != null)
128
#endregion Public Instance Methods
130
#region Internal Instance Methods
133
/// Get the PropertiesDictionary stored in the LocalDataStoreSlot for this thread.
135
/// <param name="create">create the dictionary if it does not exist, otherwise return null if is does not exist</param>
136
/// <returns>the properties for this thread</returns>
139
/// The collection returned is only to be used on the calling thread. If the
140
/// caller needs to share the collection between different threads then the
141
/// caller must clone the collection before doings so.
144
internal PropertiesDictionary GetProperties(bool create)
146
PropertiesDictionary properties = (PropertiesDictionary)CallContext.GetData("log4net.Util.LogicalThreadContextProperties");
147
if (properties == null && create)
149
properties = new PropertiesDictionary();
150
CallContext.SetData("log4net.Util.LogicalThreadContextProperties", properties);
155
#endregion Internal Instance Methods