diff --git a/README.md b/README.md index 3f231a3..e67fe4a 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,15 @@ A Serilog provider for [Microsoft.Extensions.Logging](https://www.nuget.org/packages/Microsoft.Extensions.Logging), the logging subsystem used by ASP.NET Core. -This package routes ASP.NET log messages through Serilog, so you can get information about ASP.NET's internal operations logged to the same Serilog sinks as your application events. +### ASP.NET Core 2.0+ Instructions -### Instructions +ASP.NET Core 2.0 applications should prefer [Serilog.AspNetCore](https://github.com/serilog/serilog-aspnetcore) and `UseSerilog()` instead. -**First**, install the _Serilog.Extensions.Logging_ [NuGet package](https://www.nuget.org/packages/Serilog.Extensions.Logging) into your web or console app. You will need a way to view the log messages - _Serilog.Sinks.Literate_ writes these to the console. +### ASP.NET Core 1.0, 1.1, and Default Provider Integration + +The package implements `AddSerilog()` on `ILoggingBuilder` and `ILoggerFactory` to enable the Serilog provider under the default _Microsoft.Extensions.Logging_ implementation. + +**First**, install the _Serilog.Extensions.Logging_ [NuGet package](https://www.nuget.org/packages/Serilog.Extensions.Logging) into your web or console app. You will need a way to view the log messages - _Serilog.Sinks.Console_ writes these to the console. ```powershell Install-Package Serilog.Extensions.Logging -DependencyVersion Highest @@ -32,7 +36,7 @@ public class Startup ``` **Finally**, in your `Startup` class's `Configure()` method, remove the existing logger configuration entries and -call `AddSerilog()` on the provided `loggerFactory`. +call `AddSerilog()` on the provided `loggingBuilder`. ```csharp public void ConfigureServices(IServiceCollection services) diff --git a/samples/SimpleWebSample/Controllers/ScopesController.cs b/samples/SimpleWebSample/Controllers/ScopesController.cs deleted file mode 100644 index 90fabd2..0000000 --- a/samples/SimpleWebSample/Controllers/ScopesController.cs +++ /dev/null @@ -1,36 +0,0 @@ -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.LogInformation("After"); - - return new string[] { "value1", "value2" }; - } - } -} diff --git a/samples/SimpleWebSample/Controllers/ValuesController.cs b/samples/SimpleWebSample/Controllers/ValuesController.cs deleted file mode 100644 index f14dafc..0000000 --- a/samples/SimpleWebSample/Controllers/ValuesController.cs +++ /dev/null @@ -1,19 +0,0 @@ -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/SimpleWebSample/Program.cs b/samples/SimpleWebSample/Program.cs deleted file mode 100644 index fc4d20a..0000000 --- a/samples/SimpleWebSample/Program.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.IO; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Serilog; - -namespace SimpleWebSample -{ - public class Program - { - 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() - .ReadFrom.Configuration(configuration) - .Enrich.FromLogContext() - .WriteTo.Console() - .CreateLogger(); - - try - { - Log.Information("Getting the motors running..."); - - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .UseStartup() - .UseConfiguration(configuration) - .UseSerilog() - .Build(); - - host.Run(); - - return 0; - } - catch (Exception ex) - { - Log.Fatal(ex, "Host terminated unexpectedly"); - return 1; - } - finally - { - Log.CloseAndFlush(); - } - } - } -} diff --git a/samples/SimpleWebSample/Properties/launchSettings.json b/samples/SimpleWebSample/Properties/launchSettings.json deleted file mode 100644 index d1f12a2..0000000 --- a/samples/SimpleWebSample/Properties/launchSettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "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 deleted file mode 100644 index 1e1e26e..0000000 --- a/samples/SimpleWebSample/README.md +++ /dev/null @@ -1,20 +0,0 @@ -This is a simple example illustrating the use of Serilog with a web app. - -The project was created with the following steps. - -* Create the project and add NuGet dependencies -``` -dotnet new web -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/SimpleWebSample/SimpleWebSample.csproj b/samples/SimpleWebSample/SimpleWebSample.csproj deleted file mode 100644 index d36045b..0000000 --- a/samples/SimpleWebSample/SimpleWebSample.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - netcoreapp2.0 - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/SimpleWebSample/Startup.cs b/samples/SimpleWebSample/Startup.cs deleted file mode 100644 index 84b2abc..0000000 --- a/samples/SimpleWebSample/Startup.cs +++ /dev/null @@ -1,40 +0,0 @@ -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(); - - app.Run(async (context) => - { - await context.Response.WriteAsync("Hello World!"); - }); - } - } -} diff --git a/samples/SimpleWebSample/appsettings.Development.json b/samples/SimpleWebSample/appsettings.Development.json deleted file mode 100644 index fa8ce71..0000000 --- a/samples/SimpleWebSample/appsettings.Development.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - } -} diff --git a/samples/SimpleWebSample/appsettings.json b/samples/SimpleWebSample/appsettings.json deleted file mode 100644 index b62b89a..0000000 --- a/samples/SimpleWebSample/appsettings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "Serilog": { - "MinimumLevel": { - "Default": "Debug", - "Override": { - "Microsoft": "Information", - "System": "Warning" - } - } - } -} diff --git a/serilog-extensions-logging.sln b/serilog-extensions-logging.sln index f6eb0b4..80abb55 100644 --- a/serilog-extensions-logging.sln +++ b/serilog-extensions-logging.sln @@ -24,8 +24,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9 assets\Serilog.snk = assets\Serilog.snk EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleWebSample", "samples\SimpleWebSample\SimpleWebSample.csproj", "{69F9A0ED-7910-4F33-8919-28BB05376FBC}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,10 +42,6 @@ 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 - {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -56,7 +50,6 @@ 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} - {69F9A0ED-7910-4F33-8919-28BB05376FBC} = {F2407211-6043-439C-8E06-3641634332E7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10} diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs deleted file mode 100644 index 51fa419..0000000 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs +++ /dev/null @@ -1,49 +0,0 @@ -// 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 -{ - 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 4f7a1f4..34796a1 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -1,8 +1,8 @@  - Serilog provider for Microsoft.Extensions.Logging - 2.1.0 + Low-level Serilog provider for Microsoft.Extensions.Logging + 2.0.1 Microsoft;Serilog Contributors net45;net46;net461;netstandard1.3;netstandard2.0 true @@ -29,7 +29,6 @@ - diff --git a/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs b/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs deleted file mode 100644 index fecd50e..0000000 --- a/src/Serilog.Extensions.Logging/SerilogWebHostBuilderExtensions.cs +++ /dev/null @@ -1,49 +0,0 @@ -// 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; -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