From a95aed3de69f18e3e36019afb53b26dab84a2282 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Thu, 24 Aug 2017 11:54:35 +1000 Subject: [PATCH 1/5] Fixes #98 - UseSerilog() goodness --- samples/SimpleWebAPISample/Program.cs | 51 +++++++++++------- .../Properties/launchSettings.json | 27 ++++++++++ samples/SimpleWebAPISample/README.md | 4 +- .../SimpleWebAPISample.csproj | 15 ++++-- samples/SimpleWebAPISample/appsettings.json | 16 +++--- samples/SimpleWebSample/Program.cs | 52 +++++++++++-------- .../Properties/launchSettings.json | 27 ++++++++++ samples/SimpleWebSample/README.md | 4 +- .../SimpleWebSample/SimpleWebSample.csproj | 13 ++++- .../appsettings.Development.json | 10 ++++ samples/SimpleWebSample/appsettings.json | 11 ++++ serilog-extensions-logging.sln | 32 ++++++++---- .../Logging/SerilogLoggerFactory.cs | 49 +++++++++++++++++ .../Serilog.Extensions.Logging.csproj | 2 + .../SerilogLoggingBuilderExtensions.cs | 17 +++++- .../SerilogWebHostBuilderExtensions.cs | 49 +++++++++++++++++ 16 files changed, 307 insertions(+), 72 deletions(-) create mode 100644 samples/SimpleWebAPISample/Properties/launchSettings.json create mode 100644 samples/SimpleWebSample/Properties/launchSettings.json create mode 100644 samples/SimpleWebSample/appsettings.Development.json create mode 100644 samples/SimpleWebSample/appsettings.json create mode 100644 src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs create mode 100644 src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs diff --git a/samples/SimpleWebAPISample/Program.cs b/samples/SimpleWebAPISample/Program.cs index 6db5463..5e0c4e9 100644 --- a/samples/SimpleWebAPISample/Program.cs +++ b/samples/SimpleWebAPISample/Program.cs @@ -1,39 +1,50 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; - using Serilog; namespace SimpleWebAPISample { public class Program { - public static void Main(string[] args) + public static int Main(string[] args) { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .Build(); + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) .Enrich.FromLogContext() - .WriteTo.LiterateConsole() + .WriteTo.Console() .CreateLogger(); - - Log.Information("Getting the motors running..."); - BuildWebHost(args).Run(); - } + try + { + Log.Information("Getting the motors running..."); - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging(log => - { - log.SetMinimumLevel(LogLevel.Information); - log.AddSerilog(logger: Log.Logger, dispose: true); - }) - .Build(); + var host = WebHost.CreateDefaultBuilder(args) + .UseStartup() + .UseConfiguration(configuration) + .UseSerilog() + .Build(); + + host.Run(); + + return 0; + } + catch (Exception ex) + { + Log.Fatal(ex, "Unhandled exception"); + return 1; + } + finally + { + Log.CloseAndFlush(); + } + } } } diff --git a/samples/SimpleWebAPISample/Properties/launchSettings.json b/samples/SimpleWebAPISample/Properties/launchSettings.json new file mode 100644 index 0000000..ecf1331 --- /dev/null +++ b/samples/SimpleWebAPISample/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51968/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "SimpleWebAPISample": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:51972/" + } + } +} \ No newline at end of file diff --git a/samples/SimpleWebAPISample/README.md b/samples/SimpleWebAPISample/README.md index 9790130..160dbab 100644 --- a/samples/SimpleWebAPISample/README.md +++ b/samples/SimpleWebAPISample/README.md @@ -6,7 +6,7 @@ The project was created with the following steps. ``` dotnet new webapi dotnet add package Serilog.Extensions.Logging -dotnet add package Serilog.Sinks.Literate +dotnet add package Serilog.Sinks.Console ``` * Extend the logging to include Serilog. See `Program.cs` @@ -17,4 +17,4 @@ dotnet add package Serilog.Sinks.Literate }) ``` -* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline. \ No newline at end of file +* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline. diff --git a/samples/SimpleWebAPISample/SimpleWebAPISample.csproj b/samples/SimpleWebAPISample/SimpleWebAPISample.csproj index d51b28e..7c6186c 100644 --- a/samples/SimpleWebAPISample/SimpleWebAPISample.csproj +++ b/samples/SimpleWebAPISample/SimpleWebAPISample.csproj @@ -2,15 +2,24 @@ netcoreapp2.0 + + + + + + - - + + + + - \ No newline at end of file + + diff --git a/samples/SimpleWebAPISample/appsettings.json b/samples/SimpleWebAPISample/appsettings.json index 26bb0ac..b62b89a 100644 --- a/samples/SimpleWebAPISample/appsettings.json +++ b/samples/SimpleWebAPISample/appsettings.json @@ -1,14 +1,10 @@ { - "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Warning" - } - }, - "Console": { - "LogLevel": { - "Default": "Warning" + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Information", + "System": "Warning" } } } diff --git a/samples/SimpleWebSample/Program.cs b/samples/SimpleWebSample/Program.cs index a1f519d..aa3e922 100644 --- a/samples/SimpleWebSample/Program.cs +++ b/samples/SimpleWebSample/Program.cs @@ -1,39 +1,49 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; - using Serilog; - namespace SimpleWebSample { public class Program { - public static void Main(string[] args) + public static int Main(string[] args) { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .Build(); + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) .Enrich.FromLogContext() - .WriteTo.LiterateConsole() + .WriteTo.Console() .CreateLogger(); - - Log.Information("Getting the motors running..."); - BuildWebHost(args).Run(); - } + try + { + Log.Information("Getting the motors running..."); - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .ConfigureLogging(log => - { - log.SetMinimumLevel(LogLevel.Information); - log.AddSerilog(logger: Log.Logger, dispose: true); - }) - .Build(); + var host = WebHost.CreateDefaultBuilder(args) + .UseStartup() + .UseConfiguration(configuration) + .UseSerilog() + .Build(); + + host.Run(); + + return 0; + } + catch (Exception ex) + { + Log.Fatal(ex, "Unhandled exception"); + return 1; + } + finally + { + Log.CloseAndFlush(); + } + } } } diff --git a/samples/SimpleWebSample/Properties/launchSettings.json b/samples/SimpleWebSample/Properties/launchSettings.json new file mode 100644 index 0000000..d1f12a2 --- /dev/null +++ b/samples/SimpleWebSample/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51965/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "SimpleWebSample": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:51966/" + } + } +} \ No newline at end of file diff --git a/samples/SimpleWebSample/README.md b/samples/SimpleWebSample/README.md index ef50019..1e1e26e 100644 --- a/samples/SimpleWebSample/README.md +++ b/samples/SimpleWebSample/README.md @@ -6,7 +6,7 @@ The project was created with the following steps. ``` dotnet new web dotnet add package Serilog.Extensions.Logging -dotnet add package Serilog.Sinks.Literate +dotnet add package Serilog.Sinks.Console ``` * Extend the logging to include Serilog. See `Program.cs` @@ -17,4 +17,4 @@ dotnet add package Serilog.Sinks.Literate }) ``` -* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline. \ No newline at end of file +* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline. diff --git a/samples/SimpleWebSample/SimpleWebSample.csproj b/samples/SimpleWebSample/SimpleWebSample.csproj index 0b9ba89..d36045b 100644 --- a/samples/SimpleWebSample/SimpleWebSample.csproj +++ b/samples/SimpleWebSample/SimpleWebSample.csproj @@ -2,12 +2,21 @@ netcoreapp2.0 + + + + + + - - + + + + + \ No newline at end of file diff --git a/samples/SimpleWebSample/appsettings.Development.json b/samples/SimpleWebSample/appsettings.Development.json new file mode 100644 index 0000000..fa8ce71 --- /dev/null +++ b/samples/SimpleWebSample/appsettings.Development.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/samples/SimpleWebSample/appsettings.json b/samples/SimpleWebSample/appsettings.json new file mode 100644 index 0000000..b62b89a --- /dev/null +++ b/samples/SimpleWebSample/appsettings.json @@ -0,0 +1,11 @@ +{ + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Information", + "System": "Warning" + } + } + } +} diff --git a/serilog-extensions-logging.sln b/serilog-extensions-logging.sln index 6282b71..3cca4a5 100644 --- a/serilog-extensions-logging.sln +++ b/serilog-extensions-logging.sln @@ -1,30 +1,32 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26114.2 +VisualStudioVersion = 15.0.26730.10 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Extensions.Logging", "src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj", "{903CD13A-D54B-4CEC-A55F-E22AE3D93B3B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Logging", "src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj", "{903CD13A-D54B-4CEC-A55F-E22AE3D93B3B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Extensions.Logging.Tests", "test\Serilog.Extensions.Logging.Tests\Serilog.Extensions.Logging.Tests.csproj", "{37EADF84-5E41-4224-A194-1E3299DCD0B8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Logging.Tests", "test\Serilog.Extensions.Logging.Tests\Serilog.Extensions.Logging.Tests.csproj", "{37EADF84-5E41-4224-A194-1E3299DCD0B8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{F2407211-6043-439C-8E06-3641634332E7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "samples\Sample\Sample.csproj", "{65357FBC-9BC4-466D-B621-1C3A19BC2A78}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "samples\Sample\Sample.csproj", "{65357FBC-9BC4-466D-B621-1C3A19BC2A78}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9DF-AEDD-4AA6-BEA4-912DEF3E5B8E}" ProjectSection(SolutionItems) = preProject appveyor.yml = appveyor.yml Build.ps1 = Build.ps1 + global.json = global.json 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleWebSample", "samples\SimpleWebSample\SimpleWebSample.csproj", "{69F9A0ED-7910-4F33-8919-28BB05376FBC}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleWebAPISample", "samples\SimpleWebAPISample\SimpleWebAPISample.csproj", "{010CECCE-2E92-4C3F-BED9-9AD572E170C5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -44,10 +46,14 @@ Global {65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Debug|Any CPU.Build.0 = Debug|Any CPU {65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Release|Any CPU.ActiveCfg = Release|Any CPU {65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Release|Any CPU.Build.0 = Release|Any CPU - {486DF7EB-128D-4C79-8B97-9CEFEADDB948}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {486DF7EB-128D-4C79-8B97-9CEFEADDB948}.Debug|Any CPU.Build.0 = Debug|Any CPU - {486DF7EB-128D-4C79-8B97-9CEFEADDB948}.Release|Any CPU.ActiveCfg = Release|Any CPU - {486DF7EB-128D-4C79-8B97-9CEFEADDB948}.Release|Any CPU.Build.0 = Release|Any CPU + {69F9A0ED-7910-4F33-8919-28BB05376FBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {69F9A0ED-7910-4F33-8919-28BB05376FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {69F9A0ED-7910-4F33-8919-28BB05376FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {69F9A0ED-7910-4F33-8919-28BB05376FBC}.Release|Any CPU.Build.0 = Release|Any CPU + {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -56,6 +62,10 @@ Global {903CD13A-D54B-4CEC-A55F-E22AE3D93B3B} = {A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0} {37EADF84-5E41-4224-A194-1E3299DCD0B8} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1} {65357FBC-9BC4-466D-B621-1C3A19BC2A78} = {F2407211-6043-439C-8E06-3641634332E7} - {486DF7EB-128D-4C79-8B97-9CEFEADDB948} = {F2407211-6043-439C-8E06-3641634332E7} + {69F9A0ED-7910-4F33-8919-28BB05376FBC} = {F2407211-6043-439C-8E06-3641634332E7} + {010CECCE-2E92-4C3F-BED9-9AD572E170C5} = {F2407211-6043-439C-8E06-3641634332E7} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10} EndGlobalSection EndGlobal diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs new file mode 100644 index 0000000..6e02ce4 --- /dev/null +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs @@ -0,0 +1,49 @@ +// Copyright 2017 Serilog Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if LOGGING_BUILDER + +using Microsoft.Extensions.Logging; +using Serilog.Debugging; +using Serilog.Extensions.Logging; + +namespace Serilog.Extensions.Logging.Extensions.Logging +{ + class SerilogLoggerFactory : ILoggerFactory + { + readonly SerilogLoggerProvider _provider; + + public SerilogLoggerFactory(Serilog.ILogger logger = null, bool dispose = false) + { + _provider = new SerilogLoggerProvider(logger, dispose); + } + + public void Dispose() + { + _provider.Dispose(); + } + + public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) + { + return _provider.CreateLogger(categoryName); + } + + public void AddProvider(ILoggerProvider provider) + { + SelfLog.WriteLine("Ignoring added logger provider {0}", provider); + } + } +} + +#endif // LOGGING_BUILDER diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 15c0578..08a4442 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -28,6 +28,8 @@ + + diff --git a/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs b/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs index c55602b..701fa4c 100644 --- a/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs +++ b/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs @@ -1,4 +1,18 @@ -#if LOGGING_BUILDER +// Copyright 2017 Serilog Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if LOGGING_BUILDER using System; using Microsoft.Extensions.Logging; @@ -31,4 +45,5 @@ public static ILoggingBuilder AddSerilog(this ILoggingBuilder builder, ILogger l } } } + #endif // LOGGING_BUILDER diff --git a/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs b/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs new file mode 100644 index 0000000..9bb9fd3 --- /dev/null +++ b/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs @@ -0,0 +1,49 @@ +// Copyright 2017 Serilog Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if LOGGING_BUILDER + +using System; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Logging; +using Serilog.Extensions.Logging.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; + +namespace Serilog +{ + /// + /// Extends with Serilog configuration methods. + /// + public static class SerilogWebHostBuilderExtensions + { + /// + /// Sets Serilog as the logging provider. + /// + /// The web host builder to configure. + /// 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 web host builder. + public static IWebHostBuilder UseSerilog(this IWebHostBuilder builder, Serilog.ILogger logger = null, bool dispose = false) + { + if (builder == null) throw new ArgumentNullException(nameof(builder)); + builder.ConfigureServices(collection => + collection.AddSingleton(new SerilogLoggerFactory(logger, dispose))); + return builder; + } + } +} + +#endif // LOGGING_BUILDER From ca7806d98c3b074ff3cce44d7fc28637a37992be Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Thu, 24 Aug 2017 14:41:36 +1000 Subject: [PATCH 2/5] Bump minor version - new APIs --- .../Serilog.Extensions.Logging.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 08a4442..2256166 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -2,7 +2,7 @@ Serilog provider for Microsoft.Extensions.Logging - 2.0.0 + 2.1.0 Microsoft;Serilog Contributors net45;net46;net461;netstandard1.3;netstandard2.0 true From 5602d030da8087cff675069eb3fb9d5837b3d80c Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Thu, 24 Aug 2017 15:07:44 +1000 Subject: [PATCH 3/5] Merge samples, support environment name --- samples/SimpleWebAPISample/Program.cs | 50 ------------------- .../Properties/launchSettings.json | 27 ---------- samples/SimpleWebAPISample/README.md | 20 -------- .../SimpleWebAPISample.csproj | 25 ---------- samples/SimpleWebAPISample/Startup.cs | 40 --------------- .../appsettings.Development.json | 10 ---- samples/SimpleWebAPISample/appsettings.json | 11 ---- .../Controllers/ScopesController.cs | 6 +-- .../Controllers/ValuesController.cs | 7 +-- samples/SimpleWebSample/Program.cs | 6 ++- samples/SimpleWebSample/Startup.cs | 12 ++++- serilog-extensions-logging.sln | 7 --- 12 files changed, 19 insertions(+), 202 deletions(-) delete mode 100644 samples/SimpleWebAPISample/Program.cs delete mode 100644 samples/SimpleWebAPISample/Properties/launchSettings.json delete mode 100644 samples/SimpleWebAPISample/README.md delete mode 100644 samples/SimpleWebAPISample/SimpleWebAPISample.csproj delete mode 100644 samples/SimpleWebAPISample/Startup.cs delete mode 100644 samples/SimpleWebAPISample/appsettings.Development.json delete mode 100644 samples/SimpleWebAPISample/appsettings.json rename samples/{SimpleWebAPISample => SimpleWebSample}/Controllers/ScopesController.cs (89%) rename samples/{SimpleWebAPISample => SimpleWebSample}/Controllers/ValuesController.cs (74%) diff --git a/samples/SimpleWebAPISample/Program.cs b/samples/SimpleWebAPISample/Program.cs deleted file mode 100644 index 5e0c4e9..0000000 --- a/samples/SimpleWebAPISample/Program.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.IO; -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Serilog; - -namespace SimpleWebAPISample -{ - public class Program - { - public static int Main(string[] args) - { - var configuration = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .Build(); - - Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(configuration) - .Enrich.FromLogContext() - .WriteTo.Console() - .CreateLogger(); - - try - { - Log.Information("Getting the motors running..."); - - var host = WebHost.CreateDefaultBuilder(args) - .UseStartup() - .UseConfiguration(configuration) - .UseSerilog() - .Build(); - - host.Run(); - - return 0; - } - catch (Exception ex) - { - Log.Fatal(ex, "Unhandled exception"); - return 1; - } - finally - { - Log.CloseAndFlush(); - } - } - } -} diff --git a/samples/SimpleWebAPISample/Properties/launchSettings.json b/samples/SimpleWebAPISample/Properties/launchSettings.json deleted file mode 100644 index ecf1331..0000000 --- a/samples/SimpleWebAPISample/Properties/launchSettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:51968/", - "sslPort": 0 - } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "SimpleWebAPISample": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:51972/" - } - } -} \ No newline at end of file diff --git a/samples/SimpleWebAPISample/README.md b/samples/SimpleWebAPISample/README.md deleted file mode 100644 index 160dbab..0000000 --- a/samples/SimpleWebAPISample/README.md +++ /dev/null @@ -1,20 +0,0 @@ -This is a simple example illustrating the use of Serilog with WebAPI. - -The project was created with the following steps. - -* Create the project and add NuGet dependencies -``` -dotnet new webapi -dotnet add package Serilog.Extensions.Logging -dotnet add package Serilog.Sinks.Console -``` - -* Extend the logging to include Serilog. See `Program.cs` -``` -.ConfigureLogging(log => -{ - log.AddSerilog(logger: Log.Logger, dispose: true); -}) -``` - -* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline. diff --git a/samples/SimpleWebAPISample/SimpleWebAPISample.csproj b/samples/SimpleWebAPISample/SimpleWebAPISample.csproj deleted file mode 100644 index 7c6186c..0000000 --- a/samples/SimpleWebAPISample/SimpleWebAPISample.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - netcoreapp2.0 - - - - - - - - - - - - - - - - - - - - - - diff --git a/samples/SimpleWebAPISample/Startup.cs b/samples/SimpleWebAPISample/Startup.cs deleted file mode 100644 index 059160d..0000000 --- a/samples/SimpleWebAPISample/Startup.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; - -namespace SimpleWebAPISample -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddMvc(); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseMvc(); - } - } -} diff --git a/samples/SimpleWebAPISample/appsettings.Development.json b/samples/SimpleWebAPISample/appsettings.Development.json deleted file mode 100644 index fa8ce71..0000000 --- a/samples/SimpleWebAPISample/appsettings.Development.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - } -} diff --git a/samples/SimpleWebAPISample/appsettings.json b/samples/SimpleWebAPISample/appsettings.json deleted file mode 100644 index b62b89a..0000000 --- a/samples/SimpleWebAPISample/appsettings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "Serilog": { - "MinimumLevel": { - "Default": "Debug", - "Override": { - "Microsoft": "Information", - "System": "Warning" - } - } - } -} diff --git a/samples/SimpleWebAPISample/Controllers/ScopesController.cs b/samples/SimpleWebSample/Controllers/ScopesController.cs similarity index 89% rename from samples/SimpleWebAPISample/Controllers/ScopesController.cs rename to samples/SimpleWebSample/Controllers/ScopesController.cs index 247519c..90fabd2 100644 --- a/samples/SimpleWebAPISample/Controllers/ScopesController.cs +++ b/samples/SimpleWebSample/Controllers/ScopesController.cs @@ -1,12 +1,8 @@ -using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Serilog; using Microsoft.Extensions.Logging; -namespace SimpleWebAPISample.Controllers +namespace SimpleWebSample.Controllers { [Route("api/[controller]")] public class ScopesController : Controller diff --git a/samples/SimpleWebAPISample/Controllers/ValuesController.cs b/samples/SimpleWebSample/Controllers/ValuesController.cs similarity index 74% rename from samples/SimpleWebAPISample/Controllers/ValuesController.cs rename to samples/SimpleWebSample/Controllers/ValuesController.cs index 67758ca..f14dafc 100644 --- a/samples/SimpleWebAPISample/Controllers/ValuesController.cs +++ b/samples/SimpleWebSample/Controllers/ValuesController.cs @@ -1,11 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Serilog; -namespace SimpleWebAPISample.Controllers +namespace SimpleWebSample.Controllers { [Route("api/[controller]")] public class ValuesController : Controller diff --git a/samples/SimpleWebSample/Program.cs b/samples/SimpleWebSample/Program.cs index aa3e922..6e49b29 100644 --- a/samples/SimpleWebSample/Program.cs +++ b/samples/SimpleWebSample/Program.cs @@ -13,6 +13,7 @@ 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")}.json", optional: true) .Build(); Log.Logger = new LoggerConfiguration() @@ -25,7 +26,10 @@ public static int Main(string[] args) { Log.Information("Getting the motors running..."); - var host = WebHost.CreateDefaultBuilder(args) + var host = new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() .UseStartup() .UseConfiguration(configuration) .UseSerilog() diff --git a/samples/SimpleWebSample/Startup.cs b/samples/SimpleWebSample/Startup.cs index aa8c0f2..87becee 100644 --- a/samples/SimpleWebSample/Startup.cs +++ b/samples/SimpleWebSample/Startup.cs @@ -5,16 +5,24 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace SimpleWebSample { public class Startup { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + // This method gets called by the runtime. Use this method to add services to the container. - // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { + services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -25,6 +33,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseDeveloperExceptionPage(); } + app.UseMvc(); + app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); diff --git a/serilog-extensions-logging.sln b/serilog-extensions-logging.sln index 3cca4a5..f6eb0b4 100644 --- a/serilog-extensions-logging.sln +++ b/serilog-extensions-logging.sln @@ -26,8 +26,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleWebSample", "samples\SimpleWebSample\SimpleWebSample.csproj", "{69F9A0ED-7910-4F33-8919-28BB05376FBC}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleWebAPISample", "samples\SimpleWebAPISample\SimpleWebAPISample.csproj", "{010CECCE-2E92-4C3F-BED9-9AD572E170C5}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -50,10 +48,6 @@ Global {69F9A0ED-7910-4F33-8919-28BB05376FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU {69F9A0ED-7910-4F33-8919-28BB05376FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU {69F9A0ED-7910-4F33-8919-28BB05376FBC}.Release|Any CPU.Build.0 = Release|Any CPU - {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {010CECCE-2E92-4C3F-BED9-9AD572E170C5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -63,7 +57,6 @@ Global {37EADF84-5E41-4224-A194-1E3299DCD0B8} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1} {65357FBC-9BC4-466D-B621-1C3A19BC2A78} = {F2407211-6043-439C-8E06-3641634332E7} {69F9A0ED-7910-4F33-8919-28BB05376FBC} = {F2407211-6043-439C-8E06-3641634332E7} - {010CECCE-2E92-4C3F-BED9-9AD572E170C5} = {F2407211-6043-439C-8E06-3641634332E7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10} From 341c54285cfb013a573b051f4fafe483ce61bd49 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 25 Aug 2017 08:53:09 +1000 Subject: [PATCH 4/5] Trivial sample improvements --- samples/SimpleWebSample/Program.cs | 4 ++-- samples/SimpleWebSample/Startup.cs | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/samples/SimpleWebSample/Program.cs b/samples/SimpleWebSample/Program.cs index 6e49b29..fc4d20a 100644 --- a/samples/SimpleWebSample/Program.cs +++ b/samples/SimpleWebSample/Program.cs @@ -1,9 +1,9 @@ using System; using System.IO; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Serilog; + namespace SimpleWebSample { public class Program @@ -41,7 +41,7 @@ public static int Main(string[] args) } catch (Exception ex) { - Log.Fatal(ex, "Unhandled exception"); + Log.Fatal(ex, "Host terminated unexpectedly"); return 1; } finally diff --git a/samples/SimpleWebSample/Startup.cs b/samples/SimpleWebSample/Startup.cs index 87becee..84b2abc 100644 --- a/samples/SimpleWebSample/Startup.cs +++ b/samples/SimpleWebSample/Startup.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; From 3f696913a1c05359ab46a59dfb083da657bc7955 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 25 Aug 2017 11:13:23 +1000 Subject: [PATCH 5/5] Namespace fix --- .../Extensions/Logging/SerilogLoggerFactory.cs | 2 +- .../Serilog.Extensions.Logging.csproj | 1 + .../SerilogWebHostBuilderExtensions.cs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs index 6e02ce4..51fa419 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs @@ -18,7 +18,7 @@ using Serilog.Debugging; using Serilog.Extensions.Logging; -namespace Serilog.Extensions.Logging.Extensions.Logging +namespace Serilog.Extensions.Logging { class SerilogLoggerFactory : ILoggerFactory { diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 2256166..4f7a1f4 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -17,6 +17,7 @@ https://github.com/serilog/serilog-extensions-logging http://www.apache.org/licenses/LICENSE-2.0 false + Serilog diff --git a/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs b/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs index 9bb9fd3..fecd50e 100644 --- a/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs +++ b/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs @@ -17,7 +17,7 @@ using System; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Logging; -using Serilog.Extensions.Logging.Extensions.Logging; +using Serilog.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; namespace Serilog