Skip to content

Commit 516979b

Browse files
authored
Merge pull request #43 from serilog/dev
1.1.0 Release
2 parents d70a360 + de92980 commit 516979b

File tree

13 files changed

+491
-33
lines changed

13 files changed

+491
-33
lines changed

Build.ps1

+41-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
1+
echo "build: Build started"
2+
13
Push-Location $PSScriptRoot
24

3-
if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse }
5+
if(Test-Path .\artifacts) {
6+
echo "build: Cleaning .\artifacts"
7+
Remove-Item .\artifacts -Force -Recurse
8+
}
49

5-
& dotnet restore
10+
& dotnet restore --no-cache
611

7-
$revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
12+
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
13+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
14+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"]
815

9-
Push-Location src/Serilog.Extensions.Logging
16+
echo "build: Version suffix is $suffix"
1017

11-
& dotnet pack -c Release -o ..\..\.\artifacts --version-suffix=$revision
12-
if($LASTEXITCODE -ne 0) { exit 1 }
18+
foreach ($src in ls src/*) {
19+
Push-Location $src
1320

14-
Pop-Location
15-
Push-Location test/Serilog.Extensions.Logging.Tests
21+
echo "build: Packaging project in $src"
1622

17-
& dotnet test -c Release
18-
if($LASTEXITCODE -ne 0) { exit 2 }
23+
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
24+
if($LASTEXITCODE -ne 0) { exit 1 }
25+
26+
Pop-Location
27+
}
28+
29+
foreach ($test in ls test/*.PerformanceTests) {
30+
Push-Location $test
31+
32+
echo "build: Building performance test project in $test"
33+
34+
& dotnet build -c Release
35+
if($LASTEXITCODE -ne 0) { exit 2 }
36+
37+
Pop-Location
38+
}
39+
40+
foreach ($test in ls test/*.Tests) {
41+
Push-Location $test
42+
43+
echo "build: Testing project in $test"
44+
45+
& dotnet test -c Release
46+
if($LASTEXITCODE -ne 0) { exit 3 }
47+
48+
Pop-Location
49+
}
1950

20-
Pop-Location
2151
Pop-Location

CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1.1.0
2+
* #41 - Provide a `dispose` flag to instruct provider to take ownership of the Serilog logger
3+
4+
1.0.0
5+
* Initial version
6+

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ This package routes ASP.NET log messages through Serilog, so you can get informa
1111
**First**, install the _Serilog.Extensions.Logging_ [NuGet package](https://www.nuget.org/packages/Serilog.Extensions.Logging) into your web or console app. You will need a way to view the log messages - _Serilog.Sinks.Literate_ writes these to the console.
1212

1313
```powershell
14-
Install-Package Serilog.Extensions.Logging -Pre -DependencyVersion Highest
15-
Install-Package Serilog.Sinks.Literate -Pre
14+
Install-Package Serilog.Extensions.Logging -DependencyVersion Highest
15+
Install-Package Serilog.Sinks.Literate
1616
```
1717

1818
**Next**, in your application's `Startup` method, configure Serilog first:
@@ -38,9 +38,13 @@ call `AddSerilog()` on the provided `loggerFactory`.
3838
```csharp
3939
public void Configure(IApplicationBuilder app,
4040
IHostingEnvironment env,
41-
ILoggerFactory loggerfactory)
41+
ILoggerFactory loggerfactory,
42+
IApplicationLifetime appLifetime)
4243
{
4344
loggerfactory.AddSerilog();
45+
46+
// Ensure any buffered events are sent at shutdown
47+
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
4448
```
4549

4650
That's it! With the level bumped up a little you should see log output like:

appveyor.yml

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
version: '{build}'
2+
skip_tags: true
23
image: Visual Studio 2015
34
configuration: Release
45
install:
@@ -18,5 +19,11 @@ deploy:
1819
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
1920
skip_symbols: true
2021
on:
21-
branch: /^(dev|master)$/
22-
22+
branch: /^(master|dev)$/
23+
- provider: GitHub
24+
auth_token:
25+
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
26+
artifact: /Serilog.*\.nupkg/
27+
tag: v$(appveyor_build_version)
28+
on:
29+
branch: master

samples/WebSample/Controllers/HomeController.cs

+5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Mvc;
6+
using Serilog;
67

78
namespace WebSample.Controllers
89
{
910
public class HomeController : Controller
1011
{
1112
public IActionResult Index()
1213
{
14+
Log.Information("Hello from the Index!");
15+
1316
return View();
1417
}
1518

1619
public IActionResult About()
1720
{
1821
ViewData["Message"] = "Your application description page.";
1922

23+
Log.Information("This is a handler for {Path}", Request.Path);
24+
2025
return View();
2126
}
2227

samples/WebSample/Startup.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging;
10+
using Serilog;
1011

1112
namespace WebSample
1213
{
1314
public class Startup
1415
{
1516
public Startup(IHostingEnvironment env)
1617
{
17-
var builder = new ConfigurationBuilder()
18+
Configuration = new ConfigurationBuilder()
1819
.SetBasePath(env.ContentRootPath)
1920
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
2021
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
21-
.AddEnvironmentVariables();
22-
Configuration = builder.Build();
22+
.AddEnvironmentVariables()
23+
.Build();
24+
25+
Log.Logger = new LoggerConfiguration()
26+
.ReadFrom.Configuration(Configuration)
27+
.CreateLogger();
2328
}
2429

2530
public IConfigurationRoot Configuration { get; }
@@ -34,8 +39,8 @@ public void ConfigureServices(IServiceCollection services)
3439
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3540
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
3641
{
37-
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
38-
loggerFactory.AddDebug();
42+
// Specifying dispose: true closes and flushes the Serilog `Log` class when the app shuts down.
43+
loggerFactory.AddSerilog(dispose: true);
3944

4045
if (env.IsDevelopment())
4146
{

samples/WebSample/appsettings.json

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{
2-
"Logging": {
3-
"IncludeScopes": false,
4-
"LogLevel": {
2+
"Serilog": {
3+
"MinimumLevel": {
54
"Default": "Debug",
6-
"System": "Information",
7-
"Microsoft": "Information"
8-
}
5+
"Override": {
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
},
10+
"Enrich": [
11+
"FromLogContext"
12+
],
13+
"WriteTo": [
14+
"Trace",
15+
"LiterateConsole"
16+
]
917
}
1018
}

samples/WebSample/project.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"Microsoft.Extensions.Logging.Console": "1.0.0",
2020
"Microsoft.Extensions.Logging.Debug": "1.0.0",
2121
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
22-
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
22+
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
23+
"Serilog.Extensions.Logging": { "target": "project" },
24+
"Serilog.Settings.Configuration": "2.1.0-dev-00028",
25+
"Serilog.Sinks.Trace": "2.0.0",
26+
"Serilog.Sinks.Literate": "2.0.0"
2327
},
2428

2529
"tools": {

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@ class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher
2121

2222
// May be null; if it is, Log.Logger will be lazily used
2323
readonly ILogger _logger;
24+
readonly Action _dispose;
2425

25-
public SerilogLoggerProvider(ILogger logger = null)
26+
public SerilogLoggerProvider(ILogger logger = null, bool dispose = false)
2627
{
2728
if (logger != null)
2829
_logger = logger.ForContext(new[] { this });
30+
31+
if (dispose)
32+
{
33+
if (logger != null)
34+
_dispose = () => (logger as IDisposable)?.Dispose();
35+
else
36+
_dispose = Log.CloseAndFlush;
37+
}
2938
}
3039

3140
public FrameworkLogger CreateLogger(string name)
@@ -77,6 +86,9 @@ public SerilogLoggerScope CurrentScope
7786
}
7887
#endif
7988

80-
public void Dispose() { }
89+
public void Dispose()
90+
{
91+
_dispose?.Invoke();
92+
}
8193
}
8294
}

src/Serilog.Extensions.Logging/SerilogLoggerFactoryExtensions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ public static class SerilogLoggerFactoryExtensions
1717
/// </summary>
1818
/// <param name="factory">The logger factory to configure.</param>
1919
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
20+
/// <param name="dispose">When true, dispose <paramref name="logger"/> when the framework disposes the provider. If the
21+
/// logger is not specified but <paramref name="dispose"/> is true, the <see cref="Log.CloseAndFlush()"/> method will be
22+
/// called on the static <see cref="Log"/> class instead.</param>
2023
/// <returns>The logger factory.</returns>
2124
public static ILoggerFactory AddSerilog(
2225
this ILoggerFactory factory,
23-
ILogger logger = null)
26+
ILogger logger = null,
27+
bool dispose = false)
2428
{
2529
if (factory == null) throw new ArgumentNullException(nameof(factory));
2630

27-
factory.AddProvider(new SerilogLoggerProvider(logger));
31+
factory.AddProvider(new SerilogLoggerProvider(logger, dispose));
2832

2933
return factory;
3034
}

src/Serilog.Extensions.Logging/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.0",
2+
"version": "1.1.0-*",
33
"description": "Serilog provider for Microsoft.Extensions.Logging",
44
"authors": [ "Microsoft", "Serilog Contributors" ],
55
"packOptions": {

test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.IO;
1010
using System.Linq;
1111
using Serilog.Debugging;
12+
using Serilog.Framework.Logging.Tests.Support;
1213
using Xunit;
1314

1415
namespace Serilog.Extensions.Logging.Test
@@ -269,6 +270,24 @@ public void CarriesEventIdIfNonzero()
269270
Assert.Equal(42, id.Value);
270271
}
271272

273+
[Fact]
274+
public void WhenDisposeIsFalseProvidedLoggerIsNotDisposed()
275+
{
276+
var logger = new DisposeTrackingLogger();
277+
var provider = new SerilogLoggerProvider(logger, false);
278+
provider.Dispose();
279+
Assert.False(logger.IsDisposed);
280+
}
281+
282+
[Fact]
283+
public void WhenDisposeIsTrueProvidedLoggerIsDisposed()
284+
{
285+
var logger = new DisposeTrackingLogger();
286+
var provider = new SerilogLoggerProvider(logger, true);
287+
provider.Dispose();
288+
Assert.True(logger.IsDisposed);
289+
}
290+
272291
private class FoodScope : IEnumerable<KeyValuePair<string, object>>
273292
{
274293
readonly string _name;

0 commit comments

Comments
 (0)