-
Notifications
You must be signed in to change notification settings - Fork 33
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 #11 from serilog/dev
3.0.0 Release
- Loading branch information
Showing
18 changed files
with
425 additions
and
87 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
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
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 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
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> |
46 changes: 46 additions & 0 deletions
46
src/Serilog.Extensions.Hosting/Extensions/Hosting/AmbientDiagnosticContextCollector.cs
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,46 @@ | ||
// 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; | ||
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; | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContext.cs
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,60 @@ | ||
// 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; | ||
using System.Threading; | ||
|
||
namespace Serilog.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// Implements an ambient diagnostic context using <see cref="AsyncLocal{T}"/>. | ||
/// </summary> | ||
/// <remarks>Consumers should use <see cref="IDiagnosticContext"/> to set context properties.</remarks> | ||
public sealed 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 diagnostic context. This will replace | ||
/// the active collector, if any. | ||
/// </summary> | ||
/// <returns>A collector that will receive properties added in the current diagnostic context.</returns> | ||
public DiagnosticContextCollector BeginCollection() | ||
{ | ||
return AmbientDiagnosticContextCollector.Begin(); | ||
} | ||
|
||
/// <inheritdoc cref="IDiagnosticContext.Set"/> | ||
public void Set(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.AddOrUpdate(property); | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs
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,65 @@ | ||
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 IDisposable _chainedDisposable; | ||
readonly object _propertiesLock = new object(); | ||
Dictionary<string, LogEventProperty> _properties = new Dictionary<string, LogEventProperty>(); | ||
|
||
/// <summary> | ||
/// Construct a <see cref="DiagnosticContextCollector"/>. | ||
/// </summary> | ||
/// <param name="chainedDisposable">An object that will be disposed to signal completion/disposal of | ||
/// the collector.</param> | ||
public DiagnosticContextCollector(IDisposable chainedDisposable) | ||
{ | ||
_chainedDisposable = chainedDisposable ?? throw new ArgumentNullException(nameof(chainedDisposable)); | ||
} | ||
|
||
/// <summary> | ||
/// Add the property to the context. | ||
/// </summary> | ||
/// <param name="property">The property to add.</param> | ||
public void AddOrUpdate(LogEventProperty property) | ||
{ | ||
if (property == null) throw new ArgumentNullException(nameof(property)); | ||
|
||
lock (_propertiesLock) | ||
{ | ||
if (_properties == null) return; | ||
_properties[property.Name] = 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 IEnumerable<LogEventProperty> properties) | ||
{ | ||
lock (_propertiesLock) | ||
{ | ||
properties = _properties?.Values; | ||
_properties = null; | ||
Dispose(); | ||
return properties != null; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
_chainedDisposable.Dispose(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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> | ||
/// Set the specified property on the current diagnostic context. The property will be collected | ||
/// and attached to the event emitted at the completion of the context. | ||
/// </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 Set(string propertyName, object value, bool destructureObjects = false); | ||
} | ||
} |
Oops, something went wrong.