From bd7b9fd2f40a1314196ac95e48553d9374c2f6c6 Mon Sep 17 00:00:00 2001 From: Edward Junior Date: Thu, 9 Jun 2022 01:25:25 -0300 Subject: [PATCH 1/2] Added Serilog. --- .gitignore | 1 + Core/Eisk.Core/Eisk.Core.csproj | 2 + WebApi/Eisk.WebApi/Eisk.WebApi.csproj | 4 ++ WebApi/Eisk.WebApi/Program.cs | 6 +++ .../Eisk.WebApi/appsettings.Development.json | 48 +++++++++++++++++-- WebApi/Eisk.WebApi/appsettings.json | 2 +- 6 files changed, 58 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 7260224..30fddab 100644 --- a/.gitignore +++ b/.gitignore @@ -333,3 +333,4 @@ ASALocalRun/ /build/content-repo /WebApi/Eisk.WebApi/Properties/ServiceDependencies/eisk-webapi-1 - Web Deploy/profile.arm.json /build/dnn-remote-template-render +*.txt diff --git a/Core/Eisk.Core/Eisk.Core.csproj b/Core/Eisk.Core/Eisk.Core.csproj index 56861b2..14f8dca 100644 --- a/Core/Eisk.Core/Eisk.Core.csproj +++ b/Core/Eisk.Core/Eisk.Core.csproj @@ -8,6 +8,8 @@ + + diff --git a/WebApi/Eisk.WebApi/Eisk.WebApi.csproj b/WebApi/Eisk.WebApi/Eisk.WebApi.csproj index c900c90..676bd0e 100644 --- a/WebApi/Eisk.WebApi/Eisk.WebApi.csproj +++ b/WebApi/Eisk.WebApi/Eisk.WebApi.csproj @@ -7,6 +7,10 @@ + + + + diff --git a/WebApi/Eisk.WebApi/Program.cs b/WebApi/Eisk.WebApi/Program.cs index af396cc..424ec77 100644 --- a/WebApi/Eisk.WebApi/Program.cs +++ b/WebApi/Eisk.WebApi/Program.cs @@ -6,10 +6,16 @@ using Eisk.DataServices.Interfaces; using Eisk.DomainServices; using Eisk.EFCore.Setup; +using Eisk.WebApi.Configuration; using Microsoft.OpenApi.Models; +using Serilog; var builder = WebApplication.CreateBuilder(args); + +builder.AddSerilogConfig(builder.Configuration); + +Log.Information("Configuring web host..."); // Add services to the container. builder.Services.AddControllers(); diff --git a/WebApi/Eisk.WebApi/appsettings.Development.json b/WebApi/Eisk.WebApi/appsettings.Development.json index 0c208ae..0ef72ab 100644 --- a/WebApi/Eisk.WebApi/appsettings.Development.json +++ b/WebApi/Eisk.WebApi/appsettings.Development.json @@ -1,8 +1,48 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "Console", + "DiagnosticTraceSink": { "Name": "DiagnosticTrace" } + }, + { + "Name": "File", + "Args": { "path": "Logs/log.txt" } + } + ], + "WriteTo:Async": { + "Name": "Async", + "Args": { + "configure": [ + { + "Name": "File", + "Args": { + "path": "%TEMP%/Logs/serilog-configuration-sample.txt", + "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}" + } + } + ] + } + }, + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], + "Destructure": [ + { + "Name": "ToMaximumDepth", + "Args": { "maximumDestructuringDepth": 4 } + }, + { + "Name": "ToMaximumStringLength", + "Args": { "maximumStringLength": 100 } + }, + { + "Name": "ToMaximumCollectionCount", + "Args": { "maximumCollectionCount": 10 } + } + ], + "Properties": { + "Application": "Eisk.WebApi" } } } diff --git a/WebApi/Eisk.WebApi/appsettings.json b/WebApi/Eisk.WebApi/appsettings.json index 10f68b8..23039ed 100644 --- a/WebApi/Eisk.WebApi/appsettings.json +++ b/WebApi/Eisk.WebApi/appsettings.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft.AspNetCore": "Warning" } }, From d499d34c409b88c0ff55f5edfbb018fe6ae72e1a Mon Sep 17 00:00:00 2001 From: Edward Junior Date: Thu, 9 Jun 2022 01:28:11 -0300 Subject: [PATCH 2/2] Added Serilog. --- WebApi/Eisk.WebApi/Configuration/Serilog.cs | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 WebApi/Eisk.WebApi/Configuration/Serilog.cs diff --git a/WebApi/Eisk.WebApi/Configuration/Serilog.cs b/WebApi/Eisk.WebApi/Configuration/Serilog.cs new file mode 100644 index 0000000..bde221d --- /dev/null +++ b/WebApi/Eisk.WebApi/Configuration/Serilog.cs @@ -0,0 +1,24 @@ +using Serilog; + +namespace Eisk.WebApi.Configuration +{ + public static class Serilog + { + private static readonly Func _serilogConfig = (config, env) + => new LoggerConfiguration() + .WriteTo.Seq(string.IsNullOrWhiteSpace(env["sqlServerUrl"]) ? "http://seq" : env["sqlServerUrl"]) + .WriteTo.Http(requestUri: string.IsNullOrWhiteSpace(env["requestUri"]) ? "http://logstash:8080" : env["requestUri"], queueLimitBytes: null) + .ReadFrom.Configuration(config); + + private static readonly IConfigurationRoot _configurationBuilder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true) + .Build(); + + public static void AddSerilogConfig(this WebApplicationBuilder builder, ConfigurationManager env) + => builder + .Logging + .AddSerilog(_serilogConfig(_configurationBuilder, env).CreateLogger()); + } +}