-
Notifications
You must be signed in to change notification settings - Fork 34
DiagnosticContext/IDiagnosticContext #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0dac1ce
Pull changes from https://github.com/serilog/serilog-aspnetcore/pull/…
nblumhardt 1f401ac
Fix flipped conditional
nblumhardt ac4f4cd
Diagnostic context infra for downstream middleware
nblumhardt c396419
Register diagnostic services; includes ILogger now in registered serv…
nblumhardt 414a009
Reapply some doc improvements
nblumhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"sdk": { | ||
"version": "2.1.300-*" | ||
"version": "2.2.105" | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=benaadams/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=destructure/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
32 changes: 32 additions & 0 deletions
32
src/Serilog.Extensions.Hosting/Extensions/Hosting/AmbientDiagnosticContextCollector.cs
This file contains hidden or 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,32 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Serilog.Extensions.Hosting | ||
{ | ||
class AmbientDiagnosticContextCollector : IDisposable | ||
{ | ||
static readonly AsyncLocal<AmbientDiagnosticContextCollector> AmbientCollector = | ||
new AsyncLocal<AmbientDiagnosticContextCollector>(); | ||
|
||
// The indirection here ensures that completing collection cleans up the collector in all | ||
// execution contexts. Via @benaadams' addition to `HttpContextAccessor` :-) | ||
DiagnosticContextCollector _collector; | ||
|
||
public static DiagnosticContextCollector Current => AmbientCollector.Value?._collector; | ||
|
||
public static DiagnosticContextCollector Begin() | ||
{ | ||
var value = new AmbientDiagnosticContextCollector(); | ||
value._collector = new DiagnosticContextCollector(value); | ||
AmbientCollector.Value = value; | ||
return value._collector; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_collector = null; | ||
if (AmbientCollector.Value == this) | ||
AmbientCollector.Value = null; | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContext.cs
This file contains hidden or 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,59 @@ | ||
// Copyright 2019 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
|
||
namespace Serilog.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// Implements an ambient/async-local diagnostic context. Consumers should | ||
/// use <see cref="IDiagnosticContext"/> in preference to this concrete type. | ||
/// </summary> | ||
public class DiagnosticContext : IDiagnosticContext | ||
{ | ||
readonly ILogger _logger; | ||
|
||
/// <summary> | ||
/// Construct a <see cref="DiagnosticContext"/>. | ||
/// </summary> | ||
/// <param name="logger">A logger for binding properties in the context, or <c>null</c> to use <see cref="Log.Logger"/>.</param> | ||
public DiagnosticContext(ILogger logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Start collecting properties to associate with the current async context. This will replace | ||
/// the active collector, if any. | ||
/// </summary> | ||
/// <returns>A collector that will receive events added in the current async context.</returns> | ||
public DiagnosticContextCollector BeginCollection() | ||
{ | ||
return AmbientDiagnosticContextCollector.Begin(); | ||
} | ||
|
||
/// <inheritdoc cref="IDiagnosticContext.Add"/> | ||
public void Add(string propertyName, object value, bool destructureObjects = false) | ||
{ | ||
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); | ||
|
||
var collector = AmbientDiagnosticContextCollector.Current; | ||
if (collector != null && | ||
(_logger ?? Log.Logger).BindProperty(propertyName, value, destructureObjects, out var property)) | ||
{ | ||
collector.Add(property); | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs
This file contains hidden or 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,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// A container that receives properties added to a diagnostic context. | ||
/// </summary> | ||
public sealed class DiagnosticContextCollector : IDisposable | ||
{ | ||
readonly AmbientDiagnosticContextCollector _ambientCollector; | ||
List<LogEventProperty> _properties = new List<LogEventProperty>(); | ||
|
||
internal DiagnosticContextCollector(AmbientDiagnosticContextCollector ambientCollector) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not ideal that this is |
||
{ | ||
_ambientCollector = ambientCollector ?? throw new ArgumentNullException(nameof(ambientCollector)); | ||
} | ||
|
||
/// <summary> | ||
/// Add the property to the context. | ||
/// </summary> | ||
/// <param name="property">The property to add.</param> | ||
public void Add(LogEventProperty property) | ||
{ | ||
if (property == null) throw new ArgumentNullException(nameof(property)); | ||
_properties?.Add(property); | ||
} | ||
|
||
/// <summary> | ||
/// Complete the context and retrieve the properties added to it, if any. This will | ||
/// stop collection and remove the collector from the original execution context and | ||
/// any of its children. | ||
/// </summary> | ||
/// <param name="properties">The collected properties, or null if no collection is active.</param> | ||
/// <returns>True if properties could be collected.</returns> | ||
public bool TryComplete(out List<LogEventProperty> properties) | ||
{ | ||
properties = _properties; | ||
_properties = null; | ||
Dispose(); | ||
return properties != null; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
_ambientCollector.Dispose(); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,31 @@ | ||
// Copyright 2019 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Serilog | ||
{ | ||
/// <summary> | ||
/// Collects diagnostic information for packaging into wide events. | ||
/// </summary> | ||
public interface IDiagnosticContext | ||
{ | ||
/// <summary> | ||
/// Add the specified property to the request's diagnostic payload. | ||
/// </summary> | ||
/// <param name="propertyName">The name of the property. Must be non-empty.</param> | ||
/// <param name="value">The property value.</param> | ||
/// <param name="destructureObjects">If true, the value will be serialized as structured | ||
/// data if possible; if false, the object will be recorded as a scalar or simple array.</param> | ||
void Add(string propertyName, object value, bool destructureObjects = false); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A list, rather than a dictionary, is used to save unnecessary allocations. There's a bit of a leak risk here, and despite the code jumping through a few hoops to mitigate it, we might end up wanting to pay for a dictionary and an extra allocation or two during completion.