From e68cf8d07abcfe2c22d97d56e97e9be9f8461633 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Sat, 17 Aug 2019 09:11:14 +1000 Subject: [PATCH 1/3] Include Debug sink and note this in README; includes console, file, and JSON support; fixes #107 --- README.md | 18 +++++------------- .../EarlyInitializationSample.csproj | 2 -- samples/EarlyInitializationSample/Program.cs | 1 + .../InlineInitializationSample.csproj | 2 -- samples/InlineInitializationSample/Program.cs | 1 + .../Serilog.AspNetCore.csproj | 5 +++++ 6 files changed, 12 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8ad06de..6f9a59a 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,10 @@ With _Serilog.AspNetCore_ installed and configured, you can write log messages d ### Instructions -**First**, install the _Serilog.AspNetCore_ [NuGet package](https://www.nuget.org/packages/Serilog.AspNetCore) into your app. You will need a way to view the log messages - _Serilog.Sinks.Console_ writes these to the console; there are [many more sinks available](https://www.nuget.org/packages?q=Tags%3A%22serilog%22) on NuGet. +**First**, install the _Serilog.AspNetCore_ [NuGet package](https://www.nuget.org/packages/Serilog.AspNetCore) into your app. ```shell dotnet add package Serilog.AspNetCore -dotnet add package Serilog.Sinks.Console ``` **Next**, in your application's _Program.cs_ file, configure Serilog first. A `try`/`catch` block will ensure any configuration issues are appropriately logged: @@ -79,7 +78,7 @@ That's it! With the level bumped up a little you will see log output resembling: [22:14:45.741 DBG] Handled. Status code: 304 File: /css/site.css ``` -Tip: to see Serilog output in the Visual Studio output window when running under IIS, select _ASP.NET Core Web Server_ from the _Show output from_ drop-down list. +Tip: to see Serilog output in the Visual Studio output window when running under IIS, either select _ASP.NET Core Web Server_ from the _Show output from_ drop-down list, or replace `WriteTo.Console()` in the logger configuration with `WriteTo.Debug()`. A more complete example, showing _appsettings.json_ configuration, can be found in [the sample project here](https://github.com/serilog/serilog-aspnetcore/tree/dev/samples/EarlyInitializationSample). @@ -162,17 +161,16 @@ This pattern has the advantage of reducing the number of log events that need to ### Inline initialization -You can alternatively configure Serilog inline, in `BulidWebHost()`, using a delegate as shown below: +You can alternatively configure Serilog inline, in `BuildWebHost()`, using a delegate as shown below: ```csharp - // dotnet add package Serilog.Settings.Configuration .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration .ReadFrom.Configuration(hostingContext.Configuration) .Enrich.FromLogContext() .WriteTo.Console()) ``` -This has the advantage of making the `hostingContext`'s `Configuration` object available for configuration of the logger, but at the expense of recording `Exception`s raised earlier in program startup. +This has the advantage of making the `hostingContext`'s `Configuration` object available for [configuration of the logger](https://github.com/serilog/serilog-settings-configuration), but at the expense of losing `Exception`s raised earlier in program startup. If this method is used, `Log.Logger` is assigned implicitly, and closed when the app is shut down. @@ -219,13 +217,7 @@ If [inline initialization](#inline-initialization) is used, providers can be ena ### Writing to the Azure Diagnostics Log Stream -The Azure Diagnostic Log Stream ships events from any files in the `D:\home\LogFiles\` folder. To enable this for your app, first install the _Serilog.Sinks.File_ package: - -```powershell -Install-Package Serilog.Sinks.File -``` - -Then add a file sink to your `LoggerConfiguration`, taking care to set the `shared` and `flushToDiskInterval` parameters: +The Azure Diagnostic Log Stream ships events from any files in the `D:\home\LogFiles\` folder. To enable this for your app, add a file sink to your `LoggerConfiguration`, taking care to set the `shared` and `flushToDiskInterval` parameters: ```csharp public static int Main(string[] args) diff --git a/samples/EarlyInitializationSample/EarlyInitializationSample.csproj b/samples/EarlyInitializationSample/EarlyInitializationSample.csproj index bae7e3f..aa91d7f 100644 --- a/samples/EarlyInitializationSample/EarlyInitializationSample.csproj +++ b/samples/EarlyInitializationSample/EarlyInitializationSample.csproj @@ -12,8 +12,6 @@ - - diff --git a/samples/EarlyInitializationSample/Program.cs b/samples/EarlyInitializationSample/Program.cs index e8fda83..407d45e 100644 --- a/samples/EarlyInitializationSample/Program.cs +++ b/samples/EarlyInitializationSample/Program.cs @@ -21,6 +21,7 @@ public static int Main(string[] args) Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .Enrich.FromLogContext() + .WriteTo.Debug() .WriteTo.Console( // {Properties:j} added: outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} " + diff --git a/samples/InlineInitializationSample/InlineInitializationSample.csproj b/samples/InlineInitializationSample/InlineInitializationSample.csproj index bae7e3f..aa91d7f 100644 --- a/samples/InlineInitializationSample/InlineInitializationSample.csproj +++ b/samples/InlineInitializationSample/InlineInitializationSample.csproj @@ -12,8 +12,6 @@ - - diff --git a/samples/InlineInitializationSample/Program.cs b/samples/InlineInitializationSample/Program.cs index 47f76a0..18bca7d 100644 --- a/samples/InlineInitializationSample/Program.cs +++ b/samples/InlineInitializationSample/Program.cs @@ -17,6 +17,7 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args) => .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration .ReadFrom.Configuration(hostingContext.Configuration) .Enrich.FromLogContext() + .WriteTo.Debug() .WriteTo.Console( // {Properties:j} added: outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} " + diff --git a/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj b/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj index 83af8dc..0f6174d 100644 --- a/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj +++ b/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj @@ -26,6 +26,11 @@ + + + + + From 16d1ca9e6be1a3b8cb9c20eb6418b765eb4612d0 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Sat, 17 Aug 2019 09:15:18 +1000 Subject: [PATCH 2/3] Describe how to write JSON output --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6f9a59a..300e4b3 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,9 @@ That's it! With the level bumped up a little you will see log output resembling: [22:14:45.741 DBG] Handled. Status code: 304 File: /css/site.css ``` -Tip: to see Serilog output in the Visual Studio output window when running under IIS, either select _ASP.NET Core Web Server_ from the _Show output from_ drop-down list, or replace `WriteTo.Console()` in the logger configuration with `WriteTo.Debug()`. +**Tip:** to see Serilog output in the Visual Studio output window when running under IIS, either select _ASP.NET Core Web Server_ from the _Show output from_ drop-down list, or replace `WriteTo.Console()` in the logger configuration with `WriteTo.Debug()`. -A more complete example, showing _appsettings.json_ configuration, can be found in [the sample project here](https://github.com/serilog/serilog-aspnetcore/tree/dev/samples/EarlyInitializationSample). +A more complete example, showing `appsettings.json` configuration, can be found in [the sample project here](https://github.com/serilog/serilog-aspnetcore/tree/dev/samples/EarlyInitializationSample). ### Request logging `3.0.0-*` @@ -215,6 +215,16 @@ If [inline initialization](#inline-initialization) is used, providers can be ena writeToProviders: true) ``` +### JSON output + +The `Console()`, `Debug()`, and `File()` sinks all support JSON-formatted output natively, via the included _Serilog.Formatting.Compact_ package. + +To write newline-delimited JSON, pass a `CompactJsonFormatter` or `RenderedCompactJsonFormatter` to the sink configuration method: + +```csharp + .WriteTo.Console(new RenderedCompactJsonFormatter()) +``` + ### Writing to the Azure Diagnostics Log Stream The Azure Diagnostic Log Stream ships events from any files in the `D:\home\LogFiles\` folder. To enable this for your app, add a file sink to your `LoggerConfiguration`, taking care to set the `shared` and `flushToDiskInterval` parameters: From 97c84cae6c599e0728f7460a48d27737a61ecce0 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 28 Aug 2019 07:43:04 +1000 Subject: [PATCH 3/3] Update to Serilog.Extensions.Hosting v3.0.0 --- src/Serilog.AspNetCore/Serilog.AspNetCore.csproj | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj b/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj index 0f6174d..f99b83e 100644 --- a/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj +++ b/src/Serilog.AspNetCore/Serilog.AspNetCore.csproj @@ -1,4 +1,4 @@ - + Serilog support for ASP.NET Core logging @@ -13,9 +13,9 @@ true Serilog.AspNetCore serilog;aspnet;aspnetcore - http://serilog.net/images/serilog-extension-nuget.png + https://serilog.net/images/serilog-extension-nuget.png https://github.com/serilog/serilog-aspnetcore - http://www.apache.org/licenses/LICENSE-2.0 + https://www.apache.org/licenses/LICENSE-2.0 https://github.com/serilog/serilog-aspnetcore git false @@ -24,8 +24,7 @@ - - +