From f4442ec52a32142fb93eb5297dc7782c40391166 Mon Sep 17 00:00:00 2001 From: Gert Jansen van Rensburg Date: Tue, 18 Jul 2017 09:11:14 +1000 Subject: [PATCH 1/4] Moved logging extensions to target ILoggingBuilder and updated the samples. --- samples/Sample/Program.cs | 15 ++++++++++----- samples/Sample/Sample.csproj | 10 +++++----- samples/WebSample/Startup.cs | 9 ++++++--- samples/WebSample/WebSample.csproj | 13 +++---------- serilog-extensions-logging.sln | 3 ++- .../Serilog.Extensions.Logging.csproj | 11 +++++------ ...s.cs => SerilogLoggerLogBuilderExtensions.cs} | 16 ++++++---------- 7 files changed, 37 insertions(+), 40 deletions(-) rename src/Serilog.Extensions.Logging/{SerilogLoggerServicesExtensions.cs => SerilogLoggerLogBuilderExtensions.cs} (52%) diff --git a/samples/Sample/Program.cs b/samples/Sample/Program.cs index 22eabef..49c984c 100644 --- a/samples/Sample/Program.cs +++ b/samples/Sample/Program.cs @@ -1,10 +1,11 @@ using System; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; namespace Sample { - public static class Program + public class Program { public static void Main(string[] args) { @@ -13,11 +14,15 @@ public static void Main(string[] args) .WriteTo.LiterateConsole() .CreateLogger(); - var logger = new LoggerFactory() - .AddSerilog() - .CreateLogger(typeof(Program).FullName); + var services = new ServiceCollection() + .AddLogging(builder => + { + builder.AddSerilog(); + }); - logger.LogInformation("Starting"); + var serviceProvider = services.BuildServiceProvider(); + // getting the logger using the class's name is conventional + var logger = serviceProvider.GetRequiredService>(); var startTime = DateTimeOffset.UtcNow; logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); diff --git a/samples/Sample/Sample.csproj b/samples/Sample/Sample.csproj index 7f5eabb..dc17203 100644 --- a/samples/Sample/Sample.csproj +++ b/samples/Sample/Sample.csproj @@ -1,11 +1,10 @@  - net46;netcoreapp1.0 + net46;netcoreapp2.0 Sample Exe Sample - 1.0.4 @@ -13,11 +12,12 @@ + + - - + @@ -25,4 +25,4 @@ - + \ No newline at end of file diff --git a/samples/WebSample/Startup.cs b/samples/WebSample/Startup.cs index df76f4a..4d6c493 100644 --- a/samples/WebSample/Startup.cs +++ b/samples/WebSample/Startup.cs @@ -32,6 +32,12 @@ public Startup(IHostingEnvironment env) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddLogging(builder => + { + // Specifying dispose: true closes and flushes the Serilog `Log` class when the app shuts down. + builder.AddSerilog(dispose: true); + }); + // Add framework services. services.AddMvc(); } @@ -39,9 +45,6 @@ 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) { - // Specifying dispose: true closes and flushes the Serilog `Log` class when the app shuts down. - loggerFactory.AddSerilog(dispose: true); - if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); diff --git a/samples/WebSample/WebSample.csproj b/samples/WebSample/WebSample.csproj index 2d94dc6..8b267b3 100644 --- a/samples/WebSample/WebSample.csproj +++ b/samples/WebSample/WebSample.csproj @@ -1,24 +1,20 @@  - - netcoreapp1.0 + netcoreapp2.0 true WebSample Exe WebSample 1.0.4 - PreserveNewest - - @@ -27,7 +23,7 @@ - + @@ -37,14 +33,11 @@ - - - - + \ No newline at end of file diff --git a/serilog-extensions-logging.sln b/serilog-extensions-logging.sln index b2c28fb..6282b71 100644 --- a/serilog-extensions-logging.sln +++ b/serilog-extensions-logging.sln @@ -1,4 +1,4 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26114.2 @@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9 Build.ps1 = Build.ps1 README.md = README.md assets\Serilog.snk = assets\Serilog.snk + global.json = global.json EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSample", "samples\WebSample\WebSample.csproj", "{486DF7EB-128D-4C79-8B97-9CEFEADDB948}" diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 9741108..329bab5 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -30,25 +30,24 @@ - + - - + + + - + - - diff --git a/src/Serilog.Extensions.Logging/SerilogLoggerServicesExtensions.cs b/src/Serilog.Extensions.Logging/SerilogLoggerLogBuilderExtensions.cs similarity index 52% rename from src/Serilog.Extensions.Logging/SerilogLoggerServicesExtensions.cs rename to src/Serilog.Extensions.Logging/SerilogLoggerLogBuilderExtensions.cs index fb98981..5f637c5 100644 --- a/src/Serilog.Extensions.Logging/SerilogLoggerServicesExtensions.cs +++ b/src/Serilog.Extensions.Logging/SerilogLoggerLogBuilderExtensions.cs @@ -1,6 +1,4 @@ using System; -using System.ComponentModel; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog.Extensions.Logging; @@ -8,26 +6,24 @@ namespace Serilog { /// - /// Extends with Serilog configuration methods. + /// Extends with Serilog configuration methods. /// - public static class SerilogLoggerServicesExtensions + public static class SerilogLoggerLogBuilderExtensions { /// /// Add Serilog to the logging pipeline. /// - /// The to add services to. + /// The to add logging provider to. /// 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. /// The logger factory. - public static IServiceCollection AddSerilog(this IServiceCollection services, Serilog.ILogger logger = null, bool dispose = false) + public static void AddSerilog(this ILoggingBuilder builder, ILogger logger = null, bool dispose = false) { - if (services == null) throw new ArgumentNullException(nameof(services)); + if (builder == null) throw new ArgumentNullException(nameof(builder)); - services.AddLogging(builder => builder.AddProvider(new SerilogLoggerProvider(logger, dispose))); - - return services; + builder.AddProvider(new SerilogLoggerProvider(logger, dispose)); } } } From 537628028e03d65826f25f547fee790b0926b2f3 Mon Sep 17 00:00:00 2001 From: Gert Jansen van Rensburg Date: Tue, 18 Jul 2017 10:23:55 +1000 Subject: [PATCH 2/4] Updated exception message. --- samples/Sample/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Sample/Program.cs b/samples/Sample/Program.cs index 49c984c..7f95bfa 100644 --- a/samples/Sample/Program.cs +++ b/samples/Sample/Program.cs @@ -29,7 +29,7 @@ public static void Main(string[] args) try { - throw new Exception("Boom"); + throw new Exception("Boom!"); } catch (Exception ex) { From ea1193f8cc1632a5c5cff44147067d584487c404 Mon Sep 17 00:00:00 2001 From: Gert Jansen van Rensburg Date: Tue, 18 Jul 2017 11:32:21 +1000 Subject: [PATCH 3/4] changed appveyor image to visual studio 2017 preview. --- appveyor.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d2b3156..582d479 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,13 +1,13 @@ version: '{build}' skip_tags: true -image: Visual Studio 2015 +image: Visual Studio 2017 Preview configuration: Release install: - ps: mkdir -Force ".\build\" | Out-Null - - ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/release/2.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1" - - ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli" - - ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 2.0.0-preview2-006497' - - ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path" + #- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/release/2.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1" + #- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli" + #- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 2.0.0-preview2-006497' + #- ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path" build_script: - ps: ./Build.ps1 test: off From 53a914fb1398d83b0741213a5b8074e85aab0958 Mon Sep 17 00:00:00 2001 From: Gert Jansen van Rensburg Date: Tue, 18 Jul 2017 19:43:28 +1000 Subject: [PATCH 4/4] Changed extension name to SerilogLoggingBuilderExtensions --- .../Serilog.Extensions.Logging.csproj | 4 ++-- ...uilderExtensions.cs => SerilogLoggingBuilderExtensions.cs} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/Serilog.Extensions.Logging/{SerilogLoggerLogBuilderExtensions.cs => SerilogLoggingBuilderExtensions.cs} (95%) diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 329bab5..d70fea5 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -30,7 +30,7 @@ - + @@ -44,7 +44,7 @@ - + diff --git a/src/Serilog.Extensions.Logging/SerilogLoggerLogBuilderExtensions.cs b/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs similarity index 95% rename from src/Serilog.Extensions.Logging/SerilogLoggerLogBuilderExtensions.cs rename to src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs index 5f637c5..ac375cf 100644 --- a/src/Serilog.Extensions.Logging/SerilogLoggerLogBuilderExtensions.cs +++ b/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs @@ -8,7 +8,7 @@ namespace Serilog /// /// Extends with Serilog configuration methods. /// - public static class SerilogLoggerLogBuilderExtensions + public static class SerilogLoggingBuilderExtensions { /// /// Add Serilog to the logging pipeline.