Skip to content

Commit 22d3378

Browse files
authored
Merge pull request #101 from andreycha/feat/support-peek-in-stacks
2 parents b2ef9f6 + 3899635 commit 22d3378

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

src/log4net/Util/LogicalThreadContextStack.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
namespace log4net.Util
2727
{
2828

29-
/// <summary>
29+
/// <summary>
3030
/// Delegate type used for LogicalThreadContextStack's callbacks.
3131
/// </summary>
32-
#if NET_2_0 || MONO_2_0 || NETSTANDARD
32+
#if NET_2_0 || MONO_2_0 || NETSTANDARD
3333
public delegate void TwoArgAction<T1, T2>(T1 t1, T2 t2);
34-
#else
34+
#else
3535
public delegate void TwoArgAction(string t1, LogicalThreadContextStack t2);
36-
#endif
36+
#endif
3737

38-
/// <summary>
38+
/// <summary>
3939
/// Implementation of Stack for the <see cref="log4net.LogicalThreadContext"/>
4040
/// </summary>
4141
/// <remarks>
@@ -65,7 +65,7 @@ public sealed class LogicalThreadContextStack : IFixingRequired
6565
/// </summary>
6666
#if NET_2_0 || MONO_2_0 || NETSTANDARD
6767
private TwoArgAction<string, LogicalThreadContextStack> m_registerNew;
68-
#else
68+
#else
6969
private TwoArgAction m_registerNew;
7070
#endif
7171

@@ -83,7 +83,7 @@ public sealed class LogicalThreadContextStack : IFixingRequired
8383
/// </remarks>
8484
#if NET_2_0 || MONO_2_0 || NETSTANDARD
8585
internal LogicalThreadContextStack(string propertyKey, TwoArgAction<string, LogicalThreadContextStack> registerNew)
86-
#else
86+
#else
8787
internal LogicalThreadContextStack(string propertyKey, TwoArgAction registerNew)
8888
#endif
8989
{
@@ -198,6 +198,26 @@ public IDisposable Push(string message)
198198
return new AutoPopStackFrame(contextStack, stack.Count - 1);
199199
}
200200

201+
/// <summary>
202+
/// Returns the top context from this stack.
203+
/// </summary>
204+
/// <returns>The message in the context from the top of this stack.</returns>
205+
/// <remarks>
206+
/// <para>
207+
/// Returns the top context from this stack. If this stack is empty then an
208+
/// empty string (not <see langword="null"/>) is returned.
209+
/// </para>
210+
/// </remarks>
211+
public string Peek()
212+
{
213+
Stack stack = m_stack;
214+
if (stack.Count > 0)
215+
{
216+
return ((StackFrame)stack.Peek()).Message;
217+
}
218+
return "";
219+
}
220+
201221
#endregion Public Methods
202222

203223
#region Internal Methods

src/log4net/Util/ThreadContextStack.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ public IDisposable Push(string message)
159159
return new AutoPopStackFrame(stack, stack.Count - 1);
160160
}
161161

162+
/// <summary>
163+
/// Returns the top context from this stack.
164+
/// </summary>
165+
/// <returns>The message in the context from the top of this stack.</returns>
166+
/// <remarks>
167+
/// <para>
168+
/// Returns the top context from this stack. If this stack is empty then an
169+
/// empty string (not <see langword="null"/>) is returned.
170+
/// </para>
171+
/// </remarks>
172+
public string Peek()
173+
{
174+
Stack stack = m_stack;
175+
if (stack.Count > 0)
176+
{
177+
return ((StackFrame)stack.Peek()).Message;
178+
}
179+
return "";
180+
}
181+
162182
#endregion Public Methods
163183

164184
#region Internal Methods

0 commit comments

Comments
 (0)