-
Notifications
You must be signed in to change notification settings - Fork 105
SerilogLoggerFactory and AddProvider() infrastructure #132
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
10 commits
Select commit
Hold shift + click to select a range
7030b19
Proof-of-concept AddProvider() infrastructure
nblumhardt e94a240
Lean a bit more on IoC to wire up the factory and providers
nblumhardt 1ffaa7b
Use FormattedLogValues as the event state
nblumhardt 8a9e537
Merge branch 'dev' of https://github.com/serilog/serilog-extensions-l…
nblumhardt ad31fe8
Merge upstream/dev
nblumhardt 6846f43
SerilogLogValues to clean up provision of TState; re-version as 3.0.0…
nblumhardt 1b3b885
Typos/Cleanup
nblumhardt e11cf7e
Use an SDK version present on AppVeyor :-)
nblumhardt c950770
LangVersion
nblumhardt d2155ce
Add usual level control params to WriteTo.Providers()
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
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,7 @@ | ||
<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/=destructure/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Destructurer/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=enricher/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=enrichers/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nonscalar/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
64 changes: 64 additions & 0 deletions
64
src/Serilog.Extensions.Logging/Extensions/Logging/LevelMapping.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,64 @@ | ||
// 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 Microsoft.Extensions.Logging; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Extensions.Logging | ||
{ | ||
static class LevelMapping | ||
{ | ||
public static LogEventLevel ToSerilogLevel(LogLevel logLevel) | ||
{ | ||
switch (logLevel) | ||
{ | ||
case LogLevel.Critical: | ||
return LogEventLevel.Fatal; | ||
case LogLevel.Error: | ||
return LogEventLevel.Error; | ||
case LogLevel.Warning: | ||
return LogEventLevel.Warning; | ||
case LogLevel.Information: | ||
return LogEventLevel.Information; | ||
case LogLevel.Debug: | ||
return LogEventLevel.Debug; | ||
// ReSharper disable once RedundantCaseLabel | ||
case LogLevel.Trace: | ||
default: | ||
return LogEventLevel.Verbose; | ||
} | ||
} | ||
|
||
public static LogLevel ToExtensionsLevel(LogEventLevel logEventLevel) | ||
{ | ||
switch (logEventLevel) | ||
{ | ||
case LogEventLevel.Fatal: | ||
return LogLevel.Critical; | ||
case LogEventLevel.Error: | ||
return LogLevel.Error; | ||
case LogEventLevel.Warning: | ||
return LogLevel.Warning; | ||
case LogEventLevel.Information: | ||
return LogLevel.Information; | ||
case LogEventLevel.Debug: | ||
return LogLevel.Debug; | ||
// ReSharper disable once RedundantCaseLabel | ||
case LogEventLevel.Verbose: | ||
default: | ||
return LogLevel.Trace; | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollection.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,67 @@ | ||
// 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.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Serilog.Extensions.Logging | ||
{ | ||
/// <summary> | ||
/// A dynamically-modifiable collection of <see cref="ILoggerProvider"/>s. | ||
/// </summary> | ||
public class LoggerProviderCollection : IDisposable | ||
{ | ||
volatile ILoggerProvider[] _providers = new ILoggerProvider[0]; | ||
|
||
/// <summary> | ||
/// Add <paramref name="provider"/> to the collection. | ||
/// </summary> | ||
/// <param name="provider">A logger provider.</param> | ||
public void AddProvider(ILoggerProvider provider) | ||
{ | ||
if (provider == null) throw new ArgumentNullException(nameof(provider)); | ||
|
||
var existing = _providers; | ||
var added = existing.Concat(new[] {provider}).ToArray(); | ||
|
||
#pragma warning disable 420 // ref to a volatile field | ||
while (Interlocked.CompareExchange(ref _providers, added, existing) != existing) | ||
#pragma warning restore 420 | ||
{ | ||
existing = _providers; | ||
added = existing.Concat(new[] { provider }).ToArray(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get the currently-active providers. | ||
/// </summary> | ||
/// <remarks> | ||
/// If the collection has been disposed, we'll leave the individual | ||
/// providers with the job of throwing <see cref="ObjectDisposedException"/>. | ||
/// </remarks> | ||
public IEnumerable<ILoggerProvider> Providers => _providers; | ||
|
||
/// <inheritdoc cref="IDisposable"/> | ||
public void Dispose() | ||
{ | ||
foreach (var provider in _providers) | ||
provider.Dispose(); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollectionSink.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,62 @@ | ||
// 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 Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Extensions.Logging | ||
{ | ||
class LoggerProviderCollectionSink : ILogEventSink, IDisposable | ||
{ | ||
readonly LoggerProviderCollection _providers; | ||
|
||
public LoggerProviderCollectionSink(LoggerProviderCollection providers) | ||
{ | ||
_providers = providers ?? throw new ArgumentNullException(nameof(providers)); | ||
} | ||
|
||
public void Emit(LogEvent logEvent) | ||
{ | ||
string categoryName = null; | ||
|
||
if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContextProperty) && | ||
sourceContextProperty is ScalarValue sourceContextValue && | ||
sourceContextValue.Value is string sourceContext) | ||
{ | ||
categoryName = sourceContext; | ||
} | ||
|
||
var level = LevelMapping.ToExtensionsLevel(logEvent.Level); | ||
var slv = new SerilogLogValues(logEvent.MessageTemplate, logEvent.Properties); | ||
|
||
foreach (var provider in _providers.Providers) | ||
{ | ||
var logger = provider.CreateLogger(categoryName); | ||
|
||
logger.Log( | ||
level, | ||
default, | ||
slv, | ||
logEvent.Exception, | ||
(s, e) => s.ToString()); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_providers.Dispose(); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.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,62 @@ | ||
// 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 Serilog.Events; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Serilog.Extensions.Logging | ||
{ | ||
readonly struct SerilogLogValues : IReadOnlyList<KeyValuePair<string, object>> | ||
{ | ||
// Note, this struct is only used in a very limited context internally, so we ignore | ||
// the possibility of fields being null via the default struct initialization. | ||
|
||
private readonly MessageTemplate _messageTemplate; | ||
private readonly IReadOnlyDictionary<string, LogEventPropertyValue> _properties; | ||
private readonly KeyValuePair<string, object>[] _values; | ||
|
||
public SerilogLogValues(MessageTemplate messageTemplate, IReadOnlyDictionary<string, LogEventPropertyValue> properties) | ||
{ | ||
_messageTemplate = messageTemplate ?? throw new ArgumentNullException(nameof(messageTemplate)); | ||
|
||
// The dictionary is needed for rendering through the message template | ||
_properties = properties ?? throw new ArgumentNullException(nameof(properties)); | ||
|
||
// The array is needed because the IReadOnlyList<T> interface expects indexed access | ||
_values = new KeyValuePair<string, object>[_properties.Count + 1]; | ||
var i = 0; | ||
foreach (var p in properties) | ||
{ | ||
_values[i] = new KeyValuePair<string, object>(p.Key, (p.Value is ScalarValue sv) ? sv.Value : p.Value); | ||
++i; | ||
} | ||
_values[i] = new KeyValuePair<string, object>("{OriginalFormat}", _messageTemplate.Text); | ||
} | ||
|
||
public KeyValuePair<string, object> this[int index] | ||
{ | ||
get => _values[index]; | ||
} | ||
|
||
public int Count => _properties.Count + 1; | ||
|
||
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => ((IEnumerable<KeyValuePair<string, object>>)_values).GetEnumerator(); | ||
|
||
public override string ToString() => _messageTemplate.Render(_properties); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => _values.GetEnumerator(); | ||
} | ||
} |
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.
This provider registration functionality would need to be wrapped up by
UseSerilog()
in the two alternative hosting projects.