-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from martincostello/Support-Scopes
Add support for logging scopes
- Loading branch information
Showing
5 changed files
with
239 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) Martin Costello, 2018. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
namespace MartinCostello.Logging.XUnit | ||
{ | ||
/// <summary> | ||
/// A class representing a scope for logging. This class cannot be inherited. | ||
/// </summary> | ||
internal sealed class XUnitLogScope | ||
{ | ||
/// <summary> | ||
/// The scope for the current thread. | ||
/// </summary> | ||
private static readonly AsyncLocal<XUnitLogScope> _value = new AsyncLocal<XUnitLogScope>(); | ||
|
||
/// <summary> | ||
/// The name of the logging scope. This class cannot be inherited. | ||
/// </summary> | ||
private readonly string _name; | ||
|
||
/// <summary> | ||
/// The state object for the scope. | ||
/// </summary> | ||
private readonly object _state; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="XUnitLogScope"/> class. | ||
/// </summary> | ||
/// <param name="name">The name of the logging scope.</param> | ||
/// <param name="state">The state object for the scope.</param> | ||
internal XUnitLogScope(string name, object state) | ||
{ | ||
_name = name; | ||
_state = state; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the current scope. | ||
/// </summary> | ||
internal static XUnitLogScope Current | ||
{ | ||
get { return _value.Value; } | ||
set { _value.Value = value; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets the parent scope. | ||
/// </summary> | ||
internal XUnitLogScope Parent { get; private set; } | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
=> _state.ToString(); | ||
|
||
/// <summary> | ||
/// Pushes a new value into the scope. | ||
/// </summary> | ||
/// <param name="name">The name of the logging scope.</param> | ||
/// <param name="state">The state object for the scope.</param> | ||
/// <returns> | ||
/// An <see cref="IDisposable"/> that pops the scope. | ||
/// </returns> | ||
internal static IDisposable Push(string name, object state) | ||
{ | ||
var temp = Current; | ||
|
||
Current = new XUnitLogScope(name, state) | ||
{ | ||
Parent = temp | ||
}; | ||
|
||
return new DisposableScope(); | ||
} | ||
|
||
/// <summary> | ||
/// A class the disposes of the current scope. This class cannot be inherited. | ||
/// </summary> | ||
private sealed class DisposableScope : IDisposable | ||
{ | ||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
Current = Current.Parent; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters