From 8957576813aebc68c28dd53ff9fa01c841135ab5 Mon Sep 17 00:00:00 2001 From: Abhith Date: Sat, 18 Jul 2020 13:54:56 +0400 Subject: [PATCH] feat: add option to enable elasticsearch formatter for console logs --- Samples/AspNetCoreApp/appsettings.json | 3 ++- docs/articles/aspnet-core/logging.md | 4 ++++ .../Code.Library.AspNetCore.csproj | 1 + src/Code.Library.AspNetCore/Helpers/SerilogHelper.cs | 12 ++++++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Samples/AspNetCoreApp/appsettings.json b/Samples/AspNetCoreApp/appsettings.json index 3badebd..37fa1f9 100644 --- a/Samples/AspNetCoreApp/appsettings.json +++ b/Samples/AspNetCoreApp/appsettings.json @@ -11,6 +11,7 @@ } }, "SeqServerUrl": "http://localhost:5341/", - "WriteToFile": true + "WriteToFile": true, + "UseElasticsearchFormatter": false } } \ No newline at end of file diff --git a/docs/articles/aspnet-core/logging.md b/docs/articles/aspnet-core/logging.md index 54500b8..cac04b4 100644 --- a/docs/articles/aspnet-core/logging.md +++ b/docs/articles/aspnet-core/logging.md @@ -117,6 +117,10 @@ If `SeqServerUrl` is provided (ex. "http://localhost:5341/"), then seq sink gets To enable logging to file, set it to `true`. Default value is `false`. +### UseElasticsearchFormatter + +Set this to `true` if you want your console logs to written in elasticsearch format, useful in case of hosting in k8s where EFK configured to collect logs. Default value is `false`. + We don't need the default **Logging** configuration in the **appsettings.json** anymore, remove it. ```json diff --git a/src/Code.Library.AspNetCore/Code.Library.AspNetCore.csproj b/src/Code.Library.AspNetCore/Code.Library.AspNetCore.csproj index eb2411f..a0f227b 100644 --- a/src/Code.Library.AspNetCore/Code.Library.AspNetCore.csproj +++ b/src/Code.Library.AspNetCore/Code.Library.AspNetCore.csproj @@ -26,6 +26,7 @@ Specific to ASP.NET Core project. + diff --git a/src/Code.Library.AspNetCore/Helpers/SerilogHelper.cs b/src/Code.Library.AspNetCore/Helpers/SerilogHelper.cs index c995d13..f525298 100644 --- a/src/Code.Library.AspNetCore/Helpers/SerilogHelper.cs +++ b/src/Code.Library.AspNetCore/Helpers/SerilogHelper.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Serilog.Formatting.Elasticsearch; namespace Code.Library.AspNetCore.Helpers { @@ -98,11 +99,18 @@ public static void WithSimpleConfiguration(this LoggerConfiguration loggerConfig .Enrich.WithProperty("Assembly", $"{name.Name}") .Enrich.WithProperty("Version", $"{name.Version}") .Destructure.UsingAttributes() - .WriteTo.Console() - // TODO(abhith): find alternative for TelemetryConfiguration.Active .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces); + if (configuration.GetValue("Serilog:UseElasticsearchFormatter", false)) + { + loggerConfig.WriteTo.Console(new ElasticsearchJsonFormatter()); + } + else + { + loggerConfig.WriteTo.Console(); + } + if (configuration.GetValue("Serilog:WriteToFile", false)) { loggerConfig.WriteTo.File(new RenderedCompactJsonFormatter(),