diff --git a/samples/ConfigurationWebSample/ConfigurationWebSample.csproj b/samples/ConfigurationWebSample/ConfigurationWebSample.csproj
new file mode 100644
index 0000000..9977d79
--- /dev/null
+++ b/samples/ConfigurationWebSample/ConfigurationWebSample.csproj
@@ -0,0 +1,20 @@
+
+
+ netcoreapp2.0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/ConfigurationWebSample/Controllers/ScopesController.cs b/samples/ConfigurationWebSample/Controllers/ScopesController.cs
new file mode 100644
index 0000000..66d6bbe
--- /dev/null
+++ b/samples/ConfigurationWebSample/Controllers/ScopesController.cs
@@ -0,0 +1,37 @@
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Logging;
+
+namespace SimpleWebSample.Controllers
+{
+ [Route("api/[controller]")]
+ public class ScopesController : Controller
+ {
+ ILogger _logger;
+
+ public ScopesController(ILogger logger)
+ {
+ _logger = logger;
+ }
+
+ // GET api/scopes
+ [HttpGet]
+ public IEnumerable Get()
+ {
+ _logger.LogInformation("Before");
+
+ using (_logger.BeginScope("Some name"))
+ using (_logger.BeginScope(42))
+ using (_logger.BeginScope("Formatted {WithValue}", 12345))
+ using (_logger.BeginScope(new Dictionary { ["ViaDictionary"] = 100 }))
+ {
+ _logger.LogInformation("Hello from the Index!");
+ _logger.LogDebug("Hello is done");
+ }
+
+ _logger.LogInformation("After");
+
+ return new string[] { "value1", "value2" };
+ }
+ }
+}
diff --git a/samples/ConfigurationWebSample/Controllers/ValuesController.cs b/samples/ConfigurationWebSample/Controllers/ValuesController.cs
new file mode 100644
index 0000000..f14dafc
--- /dev/null
+++ b/samples/ConfigurationWebSample/Controllers/ValuesController.cs
@@ -0,0 +1,19 @@
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Mvc;
+using Serilog;
+
+namespace SimpleWebSample.Controllers
+{
+ [Route("api/[controller]")]
+ public class ValuesController : Controller
+ {
+ // GET api/values
+ [HttpGet]
+ public IEnumerable Get()
+ {
+ // Directly through Serilog
+ Log.Information("This is a handler for {Path}", Request.Path);
+ return new string[] { "value1", "value2" };
+ }
+ }
+}
diff --git a/samples/ConfigurationWebSample/Program.cs b/samples/ConfigurationWebSample/Program.cs
new file mode 100644
index 0000000..86c226e
--- /dev/null
+++ b/samples/ConfigurationWebSample/Program.cs
@@ -0,0 +1,41 @@
+using System;
+using System.IO;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
+using Serilog;
+using Microsoft.AspNetCore;
+
+namespace SimpleWebSample
+{
+ public class Program
+ {
+ public static int Main(string[] args)
+ {
+ try
+ {
+ BuildWebHost(args).Run();
+ return 0;
+ }
+ catch (Exception ex)
+ {
+ Log.Fatal(ex, "Host terminated unexpectedly");
+ return 1;
+ }
+ finally
+ {
+ Log.CloseAndFlush();
+ }
+ }
+
+ public static IWebHost BuildWebHost(string[] args) =>
+ WebHost.CreateDefaultBuilder(args)
+ .UseStartup()
+ .UseSerilog(configuration =>
+ new Serilog.LoggerConfiguration()
+ .ReadFrom.Configuration(configuration)
+ .CreateLogger()
+ )
+ .Build();
+
+ }
+}
diff --git a/samples/ConfigurationWebSample/Startup.cs b/samples/ConfigurationWebSample/Startup.cs
new file mode 100644
index 0000000..6089e75
--- /dev/null
+++ b/samples/ConfigurationWebSample/Startup.cs
@@ -0,0 +1,35 @@
+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.
+ 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/ConfigurationWebSample/appsettings.Development.json b/samples/ConfigurationWebSample/appsettings.Development.json
new file mode 100644
index 0000000..a96c927
--- /dev/null
+++ b/samples/ConfigurationWebSample/appsettings.Development.json
@@ -0,0 +1,7 @@
+{
+ "Serilog": {
+ "MinimumLevel": {
+ "Default": "Debug"
+ }
+ }
+}
diff --git a/samples/ConfigurationWebSample/appsettings.json b/samples/ConfigurationWebSample/appsettings.json
new file mode 100644
index 0000000..7453782
--- /dev/null
+++ b/samples/ConfigurationWebSample/appsettings.json
@@ -0,0 +1,17 @@
+{
+ "Serilog": {
+ "MinimumLevel": {
+ "Default": "Debug",
+ "Override": {
+ "Microsoft": "Warning",
+ "System": "Warning"
+ }
+ },
+ "WriteTo": [
+ {
+ "Name": "Console"
+ }
+ ],
+ "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
+ }
+}
diff --git a/serilog-aspnetcore.sln b/serilog-aspnetcore.sln
index 3957472..95d254f 100644
--- a/serilog-aspnetcore.sln
+++ b/serilog-aspnetcore.sln
@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
-VisualStudioVersion = 15.0.26730.10
+VisualStudioVersion = 15.0.26730.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}"
EndProject
@@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.AspNetCore", "src\S
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.AspNetCore.Tests", "test\Serilog.AspNetCore.Tests\Serilog.AspNetCore.Tests.csproj", "{AD51759B-CD58-473F-9620-0B0E56A123A1}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigurationWebSample", "samples\ConfigurationWebSample\ConfigurationWebSample.csproj", "{527FE86F-B74A-434C-BA00-82F693260390}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -42,6 +44,10 @@ Global
{AD51759B-CD58-473F-9620-0B0E56A123A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD51759B-CD58-473F-9620-0B0E56A123A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD51759B-CD58-473F-9620-0B0E56A123A1}.Release|Any CPU.Build.0 = Release|Any CPU
+ {527FE86F-B74A-434C-BA00-82F693260390}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {527FE86F-B74A-434C-BA00-82F693260390}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {527FE86F-B74A-434C-BA00-82F693260390}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {527FE86F-B74A-434C-BA00-82F693260390}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -50,6 +56,7 @@ Global
{69F9A0ED-7910-4F33-8919-28BB05376FBC} = {F2407211-6043-439C-8E06-3641634332E7}
{0549D23F-986B-4FB2-BACE-16FD7A7BC9EF} = {A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}
{AD51759B-CD58-473F-9620-0B0E56A123A1} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
+ {527FE86F-B74A-434C-BA00-82F693260390} = {F2407211-6043-439C-8E06-3641634332E7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10}
diff --git a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs
index ad5af8c..ed6dbd1 100644
--- a/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs
+++ b/src/Serilog.AspNetCore/AspNetCore/SerilogLoggerFactory.cs
@@ -12,9 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Serilog.Debugging;
using Serilog.Extensions.Logging;
+using System;
namespace Serilog.AspNetCore
{
@@ -22,6 +24,18 @@ class SerilogLoggerFactory : ILoggerFactory
{
readonly SerilogLoggerProvider _provider;
+ public SerilogLoggerFactory(IConfiguration configuration, Func loggerBuilder, bool setCoreLogger = true, bool dispose = false)
+ {
+ var logger = loggerBuilder(configuration);
+
+ if (setCoreLogger)
+ {
+ Serilog.Log.Logger = logger;
+ }
+
+ _provider = new SerilogLoggerProvider(logger, dispose);
+ }
+
public SerilogLoggerFactory(Serilog.ILogger logger = null, bool dispose = false)
{
_provider = new SerilogLoggerProvider(logger, dispose);
diff --git a/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs b/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs
index c7a1df1..f32babb 100644
--- a/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs
+++ b/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs
@@ -17,6 +17,7 @@
using Microsoft.Extensions.Logging;
using Serilog.AspNetCore;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Configuration;
namespace Serilog
{
@@ -41,5 +42,22 @@ public static IWebHostBuilder UseSerilog(this IWebHostBuilder builder, Serilog.I
collection.AddSingleton(new SerilogLoggerFactory(logger, dispose)));
return builder;
}
+
+ ///
+ /// Sets Serilog as the logging provider from the configuration.
+ ///
+ /// The web host builder to configure.
+ /// The function to build the logger
+ /// If true, set the static from the built logger.
+ /// When true, dispose the created logger when the framework disposes the provider.
+ /// The web host builder.
+ public static IWebHostBuilder UseSerilog(this IWebHostBuilder builder, Func loggerBuilder,
+ bool setCoreLogger = true, bool dispose = false)
+ {
+ if (builder == null) throw new ArgumentNullException(nameof(builder));
+ builder.ConfigureServices((context, collection) =>
+ collection.AddSingleton(new SerilogLoggerFactory(context.Configuration, loggerBuilder, setCoreLogger, dispose)));
+ return builder;
+ }
}
}