Skip to content

Commit

Permalink
Merge pull request #43 from serilog/dev
Browse files Browse the repository at this point in the history
1.1.0 Release
  • Loading branch information
nblumhardt authored Jul 26, 2016
2 parents d70a360 + de92980 commit 516979b
Show file tree
Hide file tree
Showing 13 changed files with 491 additions and 33 deletions.
52 changes: 41 additions & 11 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
echo "build: Build started"

Push-Location $PSScriptRoot

if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse }
if(Test-Path .\artifacts) {
echo "build: Cleaning .\artifacts"
Remove-Item .\artifacts -Force -Recurse
}

& dotnet restore
& dotnet restore --no-cache

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

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

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

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

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 2 }
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
if($LASTEXITCODE -ne 0) { exit 1 }

Pop-Location
}

foreach ($test in ls test/*.PerformanceTests) {
Push-Location $test

echo "build: Building performance test project in $test"

& dotnet build -c Release
if($LASTEXITCODE -ne 0) { exit 2 }

Pop-Location
}

foreach ($test in ls test/*.Tests) {
Push-Location $test

echo "build: Testing project in $test"

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 3 }

Pop-Location
}

Pop-Location
Pop-Location
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1.1.0
* #41 - Provide a `dispose` flag to instruct provider to take ownership of the Serilog logger

1.0.0
* Initial version

10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This package routes ASP.NET log messages through Serilog, so you can get informa
**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.

```powershell
Install-Package Serilog.Extensions.Logging -Pre -DependencyVersion Highest
Install-Package Serilog.Sinks.Literate -Pre
Install-Package Serilog.Extensions.Logging -DependencyVersion Highest
Install-Package Serilog.Sinks.Literate
```

**Next**, in your application's `Startup` method, configure Serilog first:
Expand All @@ -38,9 +38,13 @@ call `AddSerilog()` on the provided `loggerFactory`.
```csharp
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerfactory)
ILoggerFactory loggerfactory,
IApplicationLifetime appLifetime)
{
loggerfactory.AddSerilog();

// Ensure any buffered events are sent at shutdown
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
```

That's it! With the level bumped up a little you should see log output like:
Expand Down
11 changes: 9 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2015
configuration: Release
install:
Expand All @@ -18,5 +19,11 @@ deploy:
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
skip_symbols: true
on:
branch: /^(dev|master)$/

branch: /^(master|dev)$/
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
artifact: /Serilog.*\.nupkg/
tag: v$(appveyor_build_version)
on:
branch: master
5 changes: 5 additions & 0 deletions samples/WebSample/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace WebSample.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
Log.Information("Hello from the Index!");

return View();
}

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

Log.Information("This is a handler for {Path}", Request.Path);

return View();
}

Expand Down
15 changes: 10 additions & 5 deletions samples/WebSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;

namespace WebSample
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
.AddEnvironmentVariables()
.Build();

Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
}

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

if (env.IsDevelopment())
{
Expand Down
20 changes: 14 additions & 6 deletions samples/WebSample/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
"Override": {
"System": "Information",
"Microsoft": "Information"
}
},
"Enrich": [
"FromLogContext"
],
"WriteTo": [
"Trace",
"LiterateConsole"
]
}
}
6 changes: 5 additions & 1 deletion samples/WebSample/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"Serilog.Extensions.Logging": { "target": "project" },
"Serilog.Settings.Configuration": "2.1.0-dev-00028",
"Serilog.Sinks.Trace": "2.0.0",
"Serilog.Sinks.Literate": "2.0.0"
},

"tools": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher

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

public SerilogLoggerProvider(ILogger logger = null)
public SerilogLoggerProvider(ILogger logger = null, bool dispose = false)
{
if (logger != null)
_logger = logger.ForContext(new[] { this });

if (dispose)
{
if (logger != null)
_dispose = () => (logger as IDisposable)?.Dispose();
else
_dispose = Log.CloseAndFlush;
}
}

public FrameworkLogger CreateLogger(string name)
Expand Down Expand Up @@ -77,6 +86,9 @@ public SerilogLoggerScope CurrentScope
}
#endif

public void Dispose() { }
public void Dispose()
{
_dispose?.Invoke();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ public static class SerilogLoggerFactoryExtensions
/// </summary>
/// <param name="factory">The logger factory to configure.</param>
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
/// <param name="dispose">When true, dispose <paramref name="logger"/> when the framework disposes the provider. If the
/// logger is not specified but <paramref name="dispose"/> is true, the <see cref="Log.CloseAndFlush()"/> method will be
/// called on the static <see cref="Log"/> class instead.</param>
/// <returns>The logger factory.</returns>
public static ILoggerFactory AddSerilog(
this ILoggerFactory factory,
ILogger logger = null)
ILogger logger = null,
bool dispose = false)
{
if (factory == null) throw new ArgumentNullException(nameof(factory));

factory.AddProvider(new SerilogLoggerProvider(logger));
factory.AddProvider(new SerilogLoggerProvider(logger, dispose));

return factory;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Extensions.Logging/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "1.1.0-*",
"description": "Serilog provider for Microsoft.Extensions.Logging",
"authors": [ "Microsoft", "Serilog Contributors" ],
"packOptions": {
Expand Down
19 changes: 19 additions & 0 deletions test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using Serilog.Debugging;
using Serilog.Framework.Logging.Tests.Support;
using Xunit;

namespace Serilog.Extensions.Logging.Test
Expand Down Expand Up @@ -269,6 +270,24 @@ public void CarriesEventIdIfNonzero()
Assert.Equal(42, id.Value);
}

[Fact]
public void WhenDisposeIsFalseProvidedLoggerIsNotDisposed()
{
var logger = new DisposeTrackingLogger();
var provider = new SerilogLoggerProvider(logger, false);
provider.Dispose();
Assert.False(logger.IsDisposed);
}

[Fact]
public void WhenDisposeIsTrueProvidedLoggerIsDisposed()
{
var logger = new DisposeTrackingLogger();
var provider = new SerilogLoggerProvider(logger, true);
provider.Dispose();
Assert.True(logger.IsDisposed);
}

private class FoodScope : IEnumerable<KeyValuePair<string, object>>
{
readonly string _name;
Expand Down
Loading

0 comments on commit 516979b

Please sign in to comment.