From c2ede67ec8a5971eaa58f934b3ed3e24f81602f7 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Mon, 23 Oct 2017 08:53:12 +1000 Subject: [PATCH 1/9] Dev version bump [Skip CI] --- src/Serilog.AspNetCore/Serilog.AspNetCore.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj b/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj index 2afb7c4..842e957 100644 --- a/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj +++ b/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj @@ -2,7 +2,7 @@ Serilog support for ASP.NET Core logging - 2.1.0 + 2.1.1 Microsoft;Serilog Contributors netstandard2.0 true From f768fcbc09043f132ae3b7e4e64c77de97ffd3dd Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Mon, 23 Oct 2017 19:36:21 +1000 Subject: [PATCH 2/9] Minor formatting tweak [Skip CI] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dab72e6..37fdfb9 100644 --- a/README.md +++ b/README.md @@ -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) => From 6db99c0ab14a62f951474beba06fefb66ab0b388 Mon Sep 17 00:00:00 2001 From: Igor Moskvitin Date: Fri, 1 Dec 2017 23:59:35 +0700 Subject: [PATCH 3/9] *Used BuildWebHost *Information via Warning --- samples/SimpleWebSample/Program.cs | 35 +++++++++++++----------- samples/SimpleWebSample/appsettings.json | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/samples/SimpleWebSample/Program.cs b/samples/SimpleWebSample/Program.cs index e080844..6c66ec8 100644 --- a/samples/SimpleWebSample/Program.cs +++ b/samples/SimpleWebSample/Program.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Serilog; @@ -8,16 +9,17 @@ 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) + .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(); @@ -26,16 +28,7 @@ public static int Main(string[] args) { Log.Information("Getting the motors running..."); - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .UseStartup() - .UseConfiguration(configuration) - .UseSerilog() - .Build(); - - host.Run(); + BuildWebHost(args).Run(); return 0; } @@ -49,5 +42,15 @@ public static int Main(string[] args) Log.CloseAndFlush(); } } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup() + .UseConfiguration(Configuration) + .UseSerilog((context, configuration) => + configuration.ReadFrom.Configuration(Configuration) + .Enrich.FromLogContext() + .WriteTo.Console()) + .Build(); } } diff --git a/samples/SimpleWebSample/appsettings.json b/samples/SimpleWebSample/appsettings.json index 4478f5c..b62b89a 100644 --- a/samples/SimpleWebSample/appsettings.json +++ b/samples/SimpleWebSample/appsettings.json @@ -3,7 +3,7 @@ "MinimumLevel": { "Default": "Debug", "Override": { - "Microsoft": "Warning", + "Microsoft": "Information", "System": "Warning" } } From d5c8bee52265e7285c82b45f3ff0232dd5ce5e4a Mon Sep 17 00:00:00 2001 From: Igor Moskvitin Date: Sun, 3 Dec 2017 20:37:13 +0700 Subject: [PATCH 4/9] Removed unnecessary config section. --- samples/SimpleWebSample/Program.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/samples/SimpleWebSample/Program.cs b/samples/SimpleWebSample/Program.cs index 6c66ec8..b4e885b 100644 --- a/samples/SimpleWebSample/Program.cs +++ b/samples/SimpleWebSample/Program.cs @@ -47,10 +47,7 @@ public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup() .UseConfiguration(Configuration) - .UseSerilog((context, configuration) => - configuration.ReadFrom.Configuration(Configuration) - .Enrich.FromLogContext() - .WriteTo.Console()) + .UseSerilog() .Build(); } } From c2fee613a107a797cdfe326197e15ef9332f42d8 Mon Sep 17 00:00:00 2001 From: Jonathan Sant Date: Sat, 9 Dec 2017 18:34:25 +0100 Subject: [PATCH 5/9] Ability to register the `SerilogLoggerFactory` I needed to retrieve some configuration prior to registering the logger. `UseSerilog` is invoked before the `Startup` class. I sought to register the Factory manually but found out that is internal. I think that `SerilogLoggerFactory` is the main take away from this library and therefore exposing it would make it more flexible for those who don't want to use the extension methods. --- src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs index ad5af8c..4deb2dd 100644 --- a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs +++ b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs @@ -18,7 +18,7 @@ namespace Serilog.AspNetCore { - class SerilogLoggerFactory : ILoggerFactory + public class SerilogLoggerFactory : ILoggerFactory { readonly SerilogLoggerProvider _provider; From 285f0f6d897c94ab685e5d03814edc2e3f2b6a50 Mon Sep 17 00:00:00 2001 From: Jonathan Sant Date: Sat, 9 Dec 2017 18:53:20 +0100 Subject: [PATCH 6/9] Added XML comments for a publicly exposed class --- .../AspNetCore/SerilogLoggerFactory.cs | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs index 4deb2dd..876e2c6 100644 --- a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs +++ b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs @@ -18,26 +18,49 @@ namespace Serilog.AspNetCore { - public class SerilogLoggerFactory : ILoggerFactory + /// + /// Implements Microsoft's ILoggerFactory so that we can inject Serilog Logger. + /// + /// + public class SerilogLoggerFactory : ILoggerFactory { readonly SerilogLoggerProvider _provider; - public SerilogLoggerFactory(Serilog.ILogger logger = null, bool dispose = false) + /// + /// Initializes a new instance of the class. + /// + /// The logger. + /// if set to true [dispose]. + public SerilogLoggerFactory(Serilog.ILogger logger = null, bool dispose = false) { _provider = new SerilogLoggerProvider(logger, dispose); } - public void Dispose() + /// + /// Disposes the provider. + /// + public void Dispose() { _provider.Dispose(); } - public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) + /// + /// Creates a new instance. + /// + /// The category name for messages produced by the logger. + /// + /// The . + /// + public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) { return _provider.CreateLogger(categoryName); } - public void AddProvider(ILoggerProvider provider) + /// + /// Adds an to the logging system. + /// + /// The . + public void AddProvider(ILoggerProvider provider) { SelfLog.WriteLine("Ignoring added logger provider {0}", provider); } From 9cf699b336cc4467866ea9f8c4aaeff49ee4e0a8 Mon Sep 17 00:00:00 2001 From: Jonathan Sant Date: Sat, 16 Dec 2017 15:04:58 +0100 Subject: [PATCH 7/9] chore(expose factory): reformatting and doc update --- .../AspNetCore/SerilogLoggerFactory.cs | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs index 876e2c6..afec510 100644 --- a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs +++ b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs @@ -18,49 +18,50 @@ namespace Serilog.AspNetCore { - /// - /// Implements Microsoft's ILoggerFactory so that we can inject Serilog Logger. - /// - /// - public class SerilogLoggerFactory : ILoggerFactory + /// + /// Implements so that we can inject Serilog Logger. + /// + public class SerilogLoggerFactory : ILoggerFactory { - readonly SerilogLoggerProvider _provider; + private readonly SerilogLoggerProvider _provider; - /// - /// Initializes a new instance of the class. - /// - /// The logger. - /// if set to true [dispose]. - public SerilogLoggerFactory(Serilog.ILogger logger = null, bool dispose = false) + /// + /// Initializes a new instance of the class. + /// + /// The Serilog logger; if not supplied, the static will be used. + /// When true, dispose when the framework disposes the provider. If the + /// logger is not specified but is true, the method will be + /// called on the static class instead. + public SerilogLoggerFactory(ILogger logger = null, bool dispose = false) { _provider = new SerilogLoggerProvider(logger, dispose); } - /// - /// Disposes the provider. - /// - public void Dispose() + /// + /// Disposes the provider. + /// + public void Dispose() { _provider.Dispose(); } - /// - /// Creates a new instance. - /// - /// The category name for messages produced by the logger. - /// - /// The . - /// - public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) + /// + /// Creates a new instance. + /// + /// The category name for messages produced by the logger. + /// + /// The . + /// + public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) { return _provider.CreateLogger(categoryName); } - /// - /// Adds an to the logging system. - /// - /// The . - public void AddProvider(ILoggerProvider provider) + /// + /// Adds an to the logging system. + /// + /// The . + public void AddProvider(ILoggerProvider provider) { SelfLog.WriteLine("Ignoring added logger provider {0}", provider); } From 92dd19eb075b91bdf6d903e7772d23332d4f6c3a Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Thu, 18 Jan 2018 14:55:34 +1000 Subject: [PATCH 8/9] Show `AddEnvironmentVariables()` in the sample code --- samples/SimpleWebSample/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/SimpleWebSample/Program.cs b/samples/SimpleWebSample/Program.cs index b4e885b..35c7cc8 100644 --- a/samples/SimpleWebSample/Program.cs +++ b/samples/SimpleWebSample/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; @@ -13,6 +13,7 @@ public class Program .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) From b883014dc8da1d1eaf534e60602dbbaf5cb5a591 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 16 Feb 2018 06:33:36 +1000 Subject: [PATCH 9/9] Add mention of configuration package to README [Skip CI] --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 37fdfb9..d8e4d47 100644 --- a/README.md +++ b/README.md @@ -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()