Skip to content

2.1.1 Release #40

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 11 commits into from
Mar 7, 2018
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Program
}
```

Then, add `UseSerilog()` to the web host builder in `BuildWebHost()`.
**Then**, add `UseSerilog()` to the web host builder in `BuildWebHost()`.

```csharp
public static IWebHost BuildWebHost(string[] args) =>
Expand Down Expand Up @@ -92,6 +92,7 @@ With _Serilog.AspNetCore_ installed and configured, you can write log messages d
You can alternatively configure Serilog using a delegate as shown below:

```csharp
// dotnet add package Serilog.Settings.Configuration
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
Expand Down
35 changes: 18 additions & 17 deletions samples/SimpleWebSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;
Expand All @@ -8,16 +9,18 @@ namespace SimpleWebSample
{
public class Program
{
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();

public static int Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.Build();

Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.ReadFrom.Configuration(Configuration)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
Expand All @@ -26,16 +29,7 @@ public static int Main(string[] args)
{
Log.Information("Getting the motors running...");

var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseConfiguration(configuration)
.UseSerilog()
.Build();

host.Run();
BuildWebHost(args).Run();

return 0;
}
Expand All @@ -49,5 +43,12 @@ public static int Main(string[] args)
Log.CloseAndFlush();
}
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseConfiguration(Configuration)
.UseSerilog()
.Build();
}
}
2 changes: 1 addition & 1 deletion samples/SimpleWebSample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"Microsoft": "Information",
"System": "Warning"
}
}
Expand Down
30 changes: 27 additions & 3 deletions src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,49 @@

namespace Serilog.AspNetCore
{
class SerilogLoggerFactory : ILoggerFactory
/// <summary>
/// Implements <see cref="ILoggerFactory"/> so that we can inject Serilog Logger.
/// </summary>
public class SerilogLoggerFactory : ILoggerFactory
{
readonly SerilogLoggerProvider _provider;
private readonly SerilogLoggerProvider _provider;

public SerilogLoggerFactory(Serilog.ILogger logger = null, bool dispose = false)
/// <summary>
/// Initializes a new instance of the <see cref="SerilogLoggerFactory"/> class.
/// </summary>
/// <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>
public SerilogLoggerFactory(ILogger logger = null, bool dispose = false)
{
_provider = new SerilogLoggerProvider(logger, dispose);
}

/// <summary>
/// Disposes the provider.
/// </summary>
public void Dispose()
{
_provider.Dispose();
}

/// <summary>
/// Creates a new <see cref="T:Microsoft.Extensions.Logging.ILogger" /> instance.
/// </summary>
/// <param name="categoryName">The category name for messages produced by the logger.</param>
/// <returns>
/// The <see cref="T:Microsoft.Extensions.Logging.ILogger" />.
/// </returns>
public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName)
{
return _provider.CreateLogger(categoryName);
}

/// <summary>
/// Adds an <see cref="T:Microsoft.Extensions.Logging.ILoggerProvider" /> to the logging system.
/// </summary>
/// <param name="provider">The <see cref="T:Microsoft.Extensions.Logging.ILoggerProvider" />.</param>
public void AddProvider(ILoggerProvider provider)
{
SelfLog.WriteLine("Ignoring added logger provider {0}", provider);
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.AspNetCore/Serilog.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Serilog support for ASP.NET Core logging</Description>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionPrefix>2.1.1</VersionPrefix>
<Authors>Microsoft;Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down