From 0a32cb12db385786bee0ee86a7c9fbe19812dcba Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Sun, 9 Jan 2022 18:23:05 +0300 Subject: [PATCH] feature(issue-124): implement Json formatter for filebeat c-gen api --- .../Controllers/CertificatesController.cs | 8 +- .../Controllers/VersionController.cs | 8 +- .../Filters/ApiExceptionFilter.cs | 2 +- .../Helpers/ExceptionExtensions.cs | 2 +- .../IoC/ServiceCollectionExtensions.cs | 4 +- .../Logging/ElasticJsonFormatter.cs | 103 ++++++++++++++++++ .../Mappings/CertificateMappings.cs | 4 +- .../Models/CertificateViewModel.cs | 2 +- ...O2NextGen.CertificateManagement.Api.csproj | 17 ++- .../Program.cs | 16 +-- .../Setup/BasicConfiguration.cs | 2 +- .../Startup.cs | 8 +- .../appsettings.json | 18 +++ .../CertificateManagementTestsStartup.cs | 2 +- 14 files changed, 165 insertions(+), 31 deletions(-) create mode 100644 src/Services/c-gen/O2NextGen.CertificateManagement.Api/Logging/ElasticJsonFormatter.cs diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/CertificatesController.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/CertificatesController.cs index 53520973..06b40c92 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/CertificatesController.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/CertificatesController.cs @@ -2,12 +2,12 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using O2NextGen.CertificateManagement.Api.Models; +using O2NextGen.CertificateManagement.Api.Setup; using O2NextGen.CertificateManagement.Business.Services; -using O2NextGen.CertificateManagement.Web.Mappings; -using O2NextGen.CertificateManagement.Web.Models; -using O2NextGen.CertificateManagement.Web.Setup; +using O2NextGen.CertificateManagement.Api.Mappings; -namespace O2NextGen.CertificateManagement.Web.Controllers +namespace O2NextGen.CertificateManagement.Api.Controllers { [Route("certificates")] public class CertificatesController : ControllerBase diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/VersionController.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/VersionController.cs index eef50250..74992e38 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/VersionController.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Controllers/VersionController.cs @@ -3,8 +3,9 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; -namespace O2NextGen.CertificateManagement.Web.Controllers +namespace O2NextGen.CertificateManagement.Api.Controllers { [AllowAnonymous] @@ -13,15 +14,17 @@ public class VersionController:ControllerBase #region Fields private readonly IHostingEnvironment _environment; + private readonly ILogger _logger; #endregion #region Ctors - public VersionController(IHostingEnvironment environment) + public VersionController(IHostingEnvironment environment, ILogger logger) { _environment = environment; + _logger = logger; } #endregion @@ -30,6 +33,7 @@ public VersionController(IHostingEnvironment environment) public object Index() { var exVersion = Assembly.GetExecutingAssembly().GetName().Version; + _logger.LogInformation($"get version - {exVersion}"); return new { Environment = _environment.EnvironmentName, diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Filters/ApiExceptionFilter.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Filters/ApiExceptionFilter.cs index 26782c02..34aecb64 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Filters/ApiExceptionFilter.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Filters/ApiExceptionFilter.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.EntityFrameworkCore; -namespace O2NextGen.CertificateManagement.Web.Filters +namespace O2NextGen.CertificateManagement.Api.Filters { public class ApiExceptionFilter: IExceptionFilter diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Helpers/ExceptionExtensions.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Helpers/ExceptionExtensions.cs index 5f3244ef..74045754 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Helpers/ExceptionExtensions.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Helpers/ExceptionExtensions.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Http; -namespace O2NextGen.CertificateManagement.Web.Helpers +namespace O2NextGen.CertificateManagement.Api.Helpers { public static class ExceptionExtensions { diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/IoC/ServiceCollectionExtensions.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/IoC/ServiceCollectionExtensions.cs index c88b30d4..e32bec21 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/IoC/ServiceCollectionExtensions.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/IoC/ServiceCollectionExtensions.cs @@ -3,12 +3,12 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using O2NextGen.CertificateManagement.Api.Filters; using O2NextGen.CertificateManagement.Business.Services; using O2NextGen.CertificateManagement.Data; using O2NextGen.CertificateManagement.Impl.Services; -using O2NextGen.CertificateManagement.Web.Filters; -namespace O2NextGen.CertificateManagement.Web.IoC +namespace O2NextGen.CertificateManagement.Api.IoC { public static class ServiceCollectionExtensions { diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Logging/ElasticJsonFormatter.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Logging/ElasticJsonFormatter.cs new file mode 100644 index 00000000..a330f4bd --- /dev/null +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Logging/ElasticJsonFormatter.cs @@ -0,0 +1,103 @@ +using System.IO; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using Serilog.Events; +using Serilog.Formatting; +using Serilog.Formatting.Json; + +namespace O2NextGen.CertificateManagement.Api.Logging +{ + public class ElasticJsonFormatter : ITextFormatter + { + private const string Prefix = "[ELASTIC]"; + + private readonly NamingStrategy namingStrategy; + private readonly JsonValueFormatter formatter; + private readonly string type; + + public ElasticJsonFormatter(NamingStrategy namingStrategy, JsonValueFormatter formatter, string type) + { + this.namingStrategy = namingStrategy; + this.formatter = formatter; + this.type = type; + } + + public ElasticJsonFormatter() + : this(new CamelCaseNamingStrategy(), new JsonValueFormatter("$type"), "commspoint") + { + } + + public void Format(LogEvent logEvent, TextWriter output) + { + output.Write(Prefix); + FormatBody(logEvent, output); + output.WriteLine(); + } + + private void FormatBody(LogEvent logEvent, TextWriter output) + { + // add json body + var writer = new JsonTextWriter(output); + writer.WriteStartObject(); + + // write level + writer.WritePropertyName("level"); + writer.WriteValue(FormatLogLevel(logEvent.Level)); + + // write type + writer.WritePropertyName("type"); + writer.WriteValue(type); + + // write timestamp + writer.WritePropertyName("timestamp"); + writer.WriteValue(logEvent.Timestamp.UtcDateTime); + + // write message + writer.WritePropertyName("message"); + writer.WriteValue(logEvent.RenderMessage()); + + // write exception if exists + if (logEvent.Exception != null) + { + writer.WritePropertyName("exception"); + writer.WriteValue(logEvent.Exception.ToString()); + } + + // write properties + writer.WritePropertyName("properties"); + writer.WriteStartObject(); + foreach (var property in logEvent.Properties) + { + var propertyName = namingStrategy.GetPropertyName(property.Key, false); + writer.WritePropertyName(propertyName); + + using (var stringWriter = new StringWriter()) + { + formatter.Format(property.Value, stringWriter); + writer.WriteRawValue(stringWriter.ToString()); + } + } + + writer.WriteEndObject(); + writer.Flush(); + } + + private static string FormatLogLevel(LogEventLevel level) + { + switch (level) + { + case LogEventLevel.Verbose: + return "TRACE"; + case LogEventLevel.Debug: + return "DEBUG"; + case LogEventLevel.Warning: + return "WARNING"; + case LogEventLevel.Error: + case LogEventLevel.Fatal: + return "ERROR"; + default: + return "INFO"; + } + } + } +} \ No newline at end of file diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Mappings/CertificateMappings.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Mappings/CertificateMappings.cs index be9f8dfa..464d5d29 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Mappings/CertificateMappings.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Mappings/CertificateMappings.cs @@ -1,10 +1,10 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using O2NextGen.CertificateManagement.Api.Models; using O2NextGen.CertificateManagement.Business.Models; -using O2NextGen.CertificateManagement.Web.Models; -namespace O2NextGen.CertificateManagement.Web.Mappings +namespace O2NextGen.CertificateManagement.Api.Mappings { public static class CertificateMappings { diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Models/CertificateViewModel.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Models/CertificateViewModel.cs index aa93cab7..08f78970 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Models/CertificateViewModel.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Models/CertificateViewModel.cs @@ -1,4 +1,4 @@ -namespace O2NextGen.CertificateManagement.Web.Models +namespace O2NextGen.CertificateManagement.Api.Models { public class CertificateViewModel { diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/O2NextGen.CertificateManagement.Api.csproj b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/O2NextGen.CertificateManagement.Api.csproj index 87b303c6..e25e25a6 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/O2NextGen.CertificateManagement.Api.csproj +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/O2NextGen.CertificateManagement.Api.csproj @@ -2,7 +2,6 @@ netcoreapp2.2 - O2NextGen.CertificateManagement.Web @@ -39,4 +38,20 @@ <_ContentIncludedByDefault Remove="Views\Certificates\Index.cshtml" /> <_ContentIncludedByDefault Remove="Views\_ViewImports.cshtml" /> + + + + + + + + + + + + + + + + diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Program.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Program.cs index fb296061..6294804b 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Program.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Program.cs @@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Hosting; using Serilog; -namespace O2NextGen.CertificateManagement.Web +namespace O2NextGen.CertificateManagement.Api { public class Program { @@ -18,15 +18,6 @@ public static async Task Main(string[] args) try { - Log.Logger = new LoggerConfiguration() - .Enrich.FromLogContext() -#if DEBUG - .WriteTo.File("Logs/system_logs.txt") -#endif - .WriteTo.Console() - - .CreateLogger(); - var host = CreateWebHostBuilder(args).Build(); Log.Information($"############### {AppName} ##############"); Log.Information("################# Starting Application #################"); @@ -48,7 +39,10 @@ public static async Task Main(string[] args) public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) - .UseSerilog() + .UseSerilog((context, configuration) => + { + configuration.ReadFrom.Configuration(context.Configuration); + }) .UseStartup(); // <- Add this line } } diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Setup/BasicConfiguration.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Setup/BasicConfiguration.cs index 3e8831fb..b35d28e0 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Setup/BasicConfiguration.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Setup/BasicConfiguration.cs @@ -1,4 +1,4 @@ -namespace O2NextGen.CertificateManagement.Web.Setup +namespace O2NextGen.CertificateManagement.Api.Setup { public class UrlsConfig { diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Startup.cs b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Startup.cs index 0cbe6560..a013de15 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Startup.cs +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/Startup.cs @@ -7,12 +7,12 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using O2NextGen.CertificateManagement.Web.Helpers; -using O2NextGen.CertificateManagement.Web.IoC; -using O2NextGen.CertificateManagement.Web.Setup; +using O2NextGen.CertificateManagement.Api.Setup; +using O2NextGen.CertificateManagement.Api.Helpers; +using O2NextGen.CertificateManagement.Api.IoC; [assembly: ApiController] -namespace O2NextGen.CertificateManagement.Web +namespace O2NextGen.CertificateManagement.Api { public class Startup { diff --git a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/appsettings.json b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/appsettings.json index f3ba4fe4..f98779d4 100644 --- a/src/Services/c-gen/O2NextGen.CertificateManagement.Api/appsettings.json +++ b/src/Services/c-gen/O2NextGen.CertificateManagement.Api/appsettings.json @@ -4,6 +4,23 @@ "Auth": "http://localhost:10001" }, "AllowedHosts": "*", + "Serilog": { + "Enrich": [ + "FromLogContext" + ], + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/log.txt", + "formatter": "O2NextGen.ESender.Api.Logging.ElasticJsonFormatter, O2NextGen.ESender.Api" + } + }, + { + "Name": "Console" + } + ] + }, "Logging": { "IncludeScopes": false, "LogLevel": { @@ -12,4 +29,5 @@ "Microsoft.Hosting.Lifetime": "Information" } } +} } \ No newline at end of file diff --git a/src/Services/c-gen/Tests/IntegrationTests.O2NextGen.CertificateManagement.Api/CertificateManagementTestsStartup.cs b/src/Services/c-gen/Tests/IntegrationTests.O2NextGen.CertificateManagement.Api/CertificateManagementTestsStartup.cs index f8af01d7..1a97bb54 100644 --- a/src/Services/c-gen/Tests/IntegrationTests.O2NextGen.CertificateManagement.Api/CertificateManagementTestsStartup.cs +++ b/src/Services/c-gen/Tests/IntegrationTests.O2NextGen.CertificateManagement.Api/CertificateManagementTestsStartup.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; -using O2NextGen.CertificateManagement.Web; +using O2NextGen.CertificateManagement.Api; namespace IntegrationTests.O2NextGen.CertificateManagement.Api {