Skip to content

Commit 71235c0

Browse files
authored
Merge pull request #145 from serilog/dev
3.0.0 Release
2 parents 73f9fb1 + 11c3c07 commit 71235c0

25 files changed

+754
-222
lines changed

appveyor.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2017 Preview
3+
image: Visual Studio 2019
44
configuration: Release
55
install:
66
- ps: mkdir -Force ".\build\" | Out-Null
@@ -16,7 +16,7 @@ artifacts:
1616
deploy:
1717
- provider: NuGet
1818
api_key:
19-
secure: bd9z4P73oltOXudAjPehwp9iDKsPtC+HbgshOrSgoyQKr5xVK+bxJQngrDJkHdY8
19+
secure: N59tiJECUYpip6tEn0xvdmDAEiP9SIzyLEFLpwiigm/8WhJvBNs13QxzT1/3/JW/
2020
skip_symbols: true
2121
on:
2222
branch: /^(master|dev)$/

build.sh

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dotnet --info
33
dotnet restore
44

55
for path in src/**/*.csproj; do
6-
dotnet build -f netstandard1.3 -c Release ${path}
76
dotnet build -f netstandard2.0 -c Release ${path}
87

98
done

global.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "2.0.0-preview2-006497"
3+
"version": "2.2.103"
44
}
55
}

samples/Sample/Program.cs

+24-7
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,41 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Logging;
44
using Serilog;
5+
using Serilog.Extensions.Logging;
56

67
namespace Sample
78
{
89
public class Program
910
{
1011
public static void Main(string[] args)
1112
{
13+
// Creating a `LoggerProviderCollection` lets Serilog optionally write
14+
// events through other dynamically-added MEL ILoggerProviders.
15+
var providers = new LoggerProviderCollection();
16+
1217
Log.Logger = new LoggerConfiguration()
1318
.MinimumLevel.Debug()
14-
.WriteTo.LiterateConsole()
19+
.WriteTo.Console()
20+
.WriteTo.Providers(providers)
1521
.CreateLogger();
1622

17-
var services = new ServiceCollection()
18-
.AddLogging(builder =>
19-
{
20-
builder.AddSerilog();
21-
});
23+
var services = new ServiceCollection();
24+
25+
services.AddSingleton(providers);
26+
services.AddSingleton<ILoggerFactory>(sc =>
27+
{
28+
var providerCollection = sc.GetService<LoggerProviderCollection>();
29+
var factory = new SerilogLoggerFactory(null, true, providerCollection);
30+
31+
foreach (var provider in sc.GetServices<ILoggerProvider>())
32+
factory.AddProvider(provider);
33+
34+
return factory;
35+
});
36+
37+
services.AddLogging(l => l.AddConsole());
2238

2339
var serviceProvider = services.BuildServiceProvider();
24-
// getting the logger using the class's name is conventional
2540
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
2641

2742
var startTime = DateTimeOffset.UtcNow;
@@ -57,6 +72,8 @@ public static void Main(string[] args)
5772
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
5873
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------");
5974
logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
75+
76+
serviceProvider.Dispose();
6077
}
6178
}
6279
}

samples/Sample/Sample.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
55
<AssemblyName>Sample</AssemblyName>
66
<OutputType>Exe</OutputType>
77
<PackageId>Sample</PackageId>
@@ -14,7 +14,8 @@
1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
1616
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
17-
<PackageReference Include="Serilog.Sinks.Literate" Version="2.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
18+
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
1819
</ItemGroup>
1920

2021
</Project>

serilog-extensions-logging.sln

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.10
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29209.62
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}"
77
EndProject
@@ -24,6 +24,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9
2424
assets\Serilog.snk = assets\Serilog.snk
2525
EndProjectSection
2626
EndProject
27+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Logging.Benchmarks", "test\Serilog.Extensions.Logging.Benchmarks\Serilog.Extensions.Logging.Benchmarks.csproj", "{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -42,6 +44,10 @@ Global
4244
{65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Debug|Any CPU.Build.0 = Debug|Any CPU
4345
{65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Release|Any CPU.ActiveCfg = Release|Any CPU
4446
{65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Release|Any CPU.Build.0 = Release|Any CPU
4551
EndGlobalSection
4652
GlobalSection(SolutionProperties) = preSolution
4753
HideSolutionNode = FALSE
@@ -50,6 +56,7 @@ Global
5056
{903CD13A-D54B-4CEC-A55F-E22AE3D93B3B} = {A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}
5157
{37EADF84-5E41-4224-A194-1E3299DCD0B8} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
5258
{65357FBC-9BC4-466D-B621-1C3A19BC2A78} = {F2407211-6043-439C-8E06-3641634332E7}
59+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
5360
EndGlobalSection
5461
GlobalSection(ExtensibilityGlobals) = postSolution
5562
SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<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">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=destructure/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/UserDictionary/Words/=destructured/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Destructurer/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Destructures/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/UserDictionary/Words/=enricher/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/UserDictionary/Words/=enrichers/@EntryIndexedValue">True</s:Boolean>
8+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Loggable/@EntryIndexedValue">True</s:Boolean>
9+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nonscalar/@EntryIndexedValue">True</s:Boolean>
10+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
11+
<s:Boolean x:Key="/Default/UserDictionary/Words/=sobj/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Microsoft.Extensions.Logging;
16+
using Serilog.Events;
17+
18+
// ReSharper disable RedundantCaseLabel
19+
20+
namespace Serilog.Extensions.Logging
21+
{
22+
/// <summary>
23+
/// Converts between Serilog and Microsoft.Extensions.Logging level enum values.
24+
/// </summary>
25+
public static class LevelConvert
26+
{
27+
/// <summary>
28+
/// Convert <paramref name="logLevel"/> to the equivalent Serilog <see cref="LogEventLevel"/>.
29+
/// </summary>
30+
/// <param name="logLevel">A Microsoft.Extensions.Logging <see cref="LogLevel"/>.</param>
31+
/// <returns>The Serilog equivalent of <paramref name="logLevel"/>.</returns>
32+
/// <remarks>The <see cref="LogLevel.None"/> value has no Serilog equivalent. It is mapped to
33+
/// <see cref="LogEventLevel.Fatal"/> as the closest approximation, but this has entirely
34+
/// different semantics.</remarks>
35+
public static LogEventLevel ToSerilogLevel(LogLevel logLevel)
36+
{
37+
switch (logLevel)
38+
{
39+
case LogLevel.None:
40+
case LogLevel.Critical:
41+
return LogEventLevel.Fatal;
42+
case LogLevel.Error:
43+
return LogEventLevel.Error;
44+
case LogLevel.Warning:
45+
return LogEventLevel.Warning;
46+
case LogLevel.Information:
47+
return LogEventLevel.Information;
48+
case LogLevel.Debug:
49+
return LogEventLevel.Debug;
50+
case LogLevel.Trace:
51+
default:
52+
return LogEventLevel.Verbose;
53+
}
54+
}
55+
56+
/// <summary>
57+
/// Convert <paramref name="logEventLevel"/> to the equivalent Microsoft.Extensions.Logging <see cref="LogLevel"/>.
58+
/// </summary>
59+
/// <param name="logEventLevel">A Serilog <see cref="LogEventLevel"/>.</param>
60+
/// <returns>The Microsoft.Extensions.Logging equivalent of <paramref name="logEventLevel"/>.</returns>
61+
public static LogLevel ToExtensionsLevel(LogEventLevel logEventLevel)
62+
{
63+
switch (logEventLevel)
64+
{
65+
case LogEventLevel.Fatal:
66+
return LogLevel.Critical;
67+
case LogEventLevel.Error:
68+
return LogLevel.Error;
69+
case LogEventLevel.Warning:
70+
return LogLevel.Warning;
71+
case LogEventLevel.Information:
72+
return LogLevel.Information;
73+
case LogEventLevel.Debug:
74+
return LogLevel.Debug;
75+
case LogEventLevel.Verbose:
76+
default:
77+
return LogLevel.Trace;
78+
}
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Threading;
20+
using Microsoft.Extensions.Logging;
21+
22+
namespace Serilog.Extensions.Logging
23+
{
24+
/// <summary>
25+
/// A dynamically-modifiable collection of <see cref="ILoggerProvider"/>s.
26+
/// </summary>
27+
public class LoggerProviderCollection : IDisposable
28+
{
29+
volatile ILoggerProvider[] _providers = new ILoggerProvider[0];
30+
31+
/// <summary>
32+
/// Add <paramref name="provider"/> to the collection.
33+
/// </summary>
34+
/// <param name="provider">A logger provider.</param>
35+
public void AddProvider(ILoggerProvider provider)
36+
{
37+
if (provider == null) throw new ArgumentNullException(nameof(provider));
38+
39+
var existing = _providers;
40+
var added = existing.Concat(new[] {provider}).ToArray();
41+
42+
#pragma warning disable 420 // ref to a volatile field
43+
while (Interlocked.CompareExchange(ref _providers, added, existing) != existing)
44+
#pragma warning restore 420
45+
{
46+
existing = _providers;
47+
added = existing.Concat(new[] { provider }).ToArray();
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Get the currently-active providers.
53+
/// </summary>
54+
/// <remarks>
55+
/// If the collection has been disposed, we'll leave the individual
56+
/// providers with the job of throwing <see cref="ObjectDisposedException"/>.
57+
/// </remarks>
58+
public IEnumerable<ILoggerProvider> Providers => _providers;
59+
60+
/// <inheritdoc cref="IDisposable"/>
61+
public void Dispose()
62+
{
63+
foreach (var provider in _providers)
64+
provider.Dispose();
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using Serilog.Core;
17+
using Serilog.Events;
18+
19+
namespace Serilog.Extensions.Logging
20+
{
21+
class LoggerProviderCollectionSink : ILogEventSink, IDisposable
22+
{
23+
readonly LoggerProviderCollection _providers;
24+
25+
public LoggerProviderCollectionSink(LoggerProviderCollection providers)
26+
{
27+
_providers = providers ?? throw new ArgumentNullException(nameof(providers));
28+
}
29+
30+
public void Emit(LogEvent logEvent)
31+
{
32+
string categoryName = null;
33+
34+
if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContextProperty) &&
35+
sourceContextProperty is ScalarValue sourceContextValue &&
36+
sourceContextValue.Value is string sourceContext)
37+
{
38+
categoryName = sourceContext;
39+
}
40+
41+
var level = LevelConvert.ToExtensionsLevel(logEvent.Level);
42+
var slv = new SerilogLogValues(logEvent.MessageTemplate, logEvent.Properties);
43+
44+
foreach (var provider in _providers.Providers)
45+
{
46+
var logger = provider.CreateLogger(categoryName);
47+
48+
logger.Log(
49+
level,
50+
default,
51+
slv,
52+
logEvent.Exception,
53+
(s, e) => s.ToString());
54+
}
55+
}
56+
57+
public void Dispose()
58+
{
59+
_providers.Dispose();
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)