Skip to content

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 5 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "2.1.300-*"
"version": "2.2.105"
}
}
2 changes: 1 addition & 1 deletion samples/SimpleServiceSample/PrintTimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace SimpleWebSample
namespace SimpleServiceSample
{
public class PrintTimeService : IHostedService, IDisposable
{
Expand Down
6 changes: 3 additions & 3 deletions samples/SimpleServiceSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.IO;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace SimpleWebSample
namespace SimpleServiceSample
{
public class Program
{
Expand Down
5 changes: 5 additions & 0 deletions serilog-extensions-hosting.sln.DotSettings
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>
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;
}
}
}
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);
}
}
}
}
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>();
Copy link
Member Author

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.


internal DiagnosticContextCollector(AmbientDiagnosticContextCollector ambientCollector)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ideal that this is internal, but it does save exposing some otherwise useless types.

{
_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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.ComponentModel;
using Microsoft.Extensions.Logging;
using Serilog.Debugging;
using Serilog.Extensions.Logging;

// To line up with the convention used elsewhere in the *.Extensions libraries, this
// should have been Serilog.Extensions.Hosting.
// ReSharper disable once CheckNamespace
namespace Serilog.Hosting
{
/// <summary>
/// Implements <see cref="ILoggerFactory"/> so that we can inject Serilog Logger.
/// </summary>
[Obsolete("Replaced with Serilog.Extensions.Logging.SerilogLoggerFactory")]
[EditorBrowsable(EditorBrowsableState.Never)]
public class SerilogLoggerFactory : ILoggerFactory
{
private readonly SerilogLoggerProvider _provider;
readonly SerilogLoggerProvider _provider;

/// <summary>
/// Initializes a new instance of the <see cref="SerilogLoggerFactory"/> class.
Expand Down
31 changes: 31 additions & 0 deletions src/Serilog.Extensions.Hosting/IDiagnosticContext.cs
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Serilog support for .NET Core logging in hosted services</Description>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>3.0.0</VersionPrefix>
<Authors>Microsoft;Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -23,8 +23,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.0-*" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.0" />
Expand Down
Loading