From e9d30aad16b58366923b39729b17c63d629f9ecf Mon Sep 17 00:00:00 2001 From: Roland Fieger Date: Tue, 12 Oct 2021 16:42:24 +0200 Subject: [PATCH] OP and Logging to Elasticsearch --- .vscode/launch.json | 2 +- BusinessLogic/LookupLogic/FwLookupLogic.cs | 1 + SearchWebApi/Controllers/LookupController.cs | 2 +- SearchWebApi/Program.cs | 15 ++++++++- SearchWebApi/SearchWebApi.csproj | 8 ++++- SearchWebApi/Startup.cs | 30 +++++++----------- SearchWebApi/appsettings.Development.json | 32 +++++++++++--------- SearchWebApi/appsettings.json | 32 +++++++++++++++++--- 8 files changed, 81 insertions(+), 41 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index cca8f6c..32c537c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/SearchWebApi/bin/Debug/netcoreapp3.1/SearchWebApi.dll", + "program": "${workspaceFolder}/SearchWebApi/bin/Debug/net6.0/SearchWebApi.dll", "args": [], "cwd": "${workspaceFolder}/SearchWebApi", "stopAtEntry": false, diff --git a/BusinessLogic/LookupLogic/FwLookupLogic.cs b/BusinessLogic/LookupLogic/FwLookupLogic.cs index 3aa9d42..61486a7 100644 --- a/BusinessLogic/LookupLogic/FwLookupLogic.cs +++ b/BusinessLogic/LookupLogic/FwLookupLogic.cs @@ -27,6 +27,7 @@ namespace BusinessLogic.LookupLogic public async Task>> GetAllLookups() { var result = await _lookupRepo.GetAllLookups(); + _logger.LogInformation($"Call to GetAllLookups ended with {nameof(result.GeneralResult)}"); return result; } diff --git a/SearchWebApi/Controllers/LookupController.cs b/SearchWebApi/Controllers/LookupController.cs index 922de00..c98ca38 100644 --- a/SearchWebApi/Controllers/LookupController.cs +++ b/SearchWebApi/Controllers/LookupController.cs @@ -29,7 +29,7 @@ namespace FwSearchApi.Controllers if (result.GeneralResult != Datamodels.BusinessModels.GeneralResults.Success) { throw new System.Exception("das war wohl nix"); } - _logger.LogInformation($"Lookups zur Verf�gung gestellt von {nameof(GetAllLookups)}"); + _logger.LogInformation($"Lookups zur Verfügung gestellt von {nameof(GetAllLookups)}"); return new JsonResult(result); } diff --git a/SearchWebApi/Program.cs b/SearchWebApi/Program.cs index e6b86b0..7213795 100644 --- a/SearchWebApi/Program.cs +++ b/SearchWebApi/Program.cs @@ -24,7 +24,17 @@ namespace FwSearchApi public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(Configuration) + .CreateLogger(); + + try { + CreateHostBuilder(args).Build().Run(); + } + finally + { + Log.CloseAndFlush(); + } } public static IHostBuilder CreateHostBuilder(string[] args) => @@ -32,12 +42,15 @@ namespace FwSearchApi .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); + webBuilder.UseSerilog(); webBuilder.UseConfiguration(Configuration); + webBuilder.SuppressStatusMessages(true); }) .ConfigureLogging((context, builder) => { builder.ClearProviders(); builder.AddConsole(); + builder.AddSerilog(); builder.AddOpenTelemetry(options => { diff --git a/SearchWebApi/SearchWebApi.csproj b/SearchWebApi/SearchWebApi.csproj index 7e111a0..f528809 100644 --- a/SearchWebApi/SearchWebApi.csproj +++ b/SearchWebApi/SearchWebApi.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net6.0 10 @@ -17,7 +17,13 @@ + + + + + + diff --git a/SearchWebApi/Startup.cs b/SearchWebApi/Startup.cs index 4d18342..6b5b176 100644 --- a/SearchWebApi/Startup.cs +++ b/SearchWebApi/Startup.cs @@ -1,27 +1,22 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; + using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Repositories; -using Repositories.Interfaces; + using BusinessLogic.LookupLogic; using BusinessLogic.LookupLogic.Interfaces; -using SearchLogic; -using OpenTelemetry.Exporter; -using OpenTelemetry.Instrumentation.AspNetCore; + using OpenTelemetry.Resources; using OpenTelemetry.Trace; -using Elastic.Apm.AspNetCore; -using Elastic.Apm.EntityFrameworkCore; -using Elastic.Apm; + +using Repositories; +using Repositories.Interfaces; + +using SearchLogic; namespace FwSearchApi { @@ -50,13 +45,12 @@ namespace FwSearchApi services.AddTransient(); services.AddTransient(); services.AddSwaggerGen(); - + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); //needed if no Security is used when calling APM Server services.AddOpenTelemetryTracing((builder) => builder - //.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("TxSearchApi")) + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("TxSearchApi")) .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() - .AddEntityFrameworkCoreInstrumentation() - .AddConsoleExporter() + .AddEntityFrameworkCoreInstrumentation(o => o.SetDbStatementForText = true) //logging of Statement as Span .AddOtlpExporter(otlpOptions => { otlpOptions.Endpoint = new Uri("http://192.168.178.44:8200"); })); @@ -75,8 +69,6 @@ namespace FwSearchApi app.UseSwagger(); - //app.UseElasticApm(_configuration, new EfCoreDiagnosticsSubscriber()); - app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "FeatureWerk Search API V1"); diff --git a/SearchWebApi/appsettings.Development.json b/SearchWebApi/appsettings.Development.json index 38e9150..d938ced 100644 --- a/SearchWebApi/appsettings.Development.json +++ b/SearchWebApi/appsettings.Development.json @@ -1,8 +1,8 @@ { "Logging": { "LogLevel": { - "Default": "Warning", - "Microsoft": "Warning", + "Default": "Information", + "Microsoft": "Information", "Microsoft.Hosting.Lifetime": "Information" } }, @@ -17,24 +17,28 @@ }, "Serilog": { "MinimumLevel": { - "Default": "Information", - "Override": { + "Default": "Information", + "Override": { "System": "Information", - "Microsoft": "Warning" + "Microsoft": "Information" } }, "WriteTo": [ - { "Name": "File", - "Args": { - "path": "/tmp/FwSearchApi.log", - "rollingInterval": "Day" - } - } + { + "Name": "Elasticsearch", + "Args": { + "nodeUris": "http://192.168.178.44:9200", + "indexFormat": "TextorSearchLoggingIndex-{0:yyyy.MM}", + "autoRegisterTemplate": true, + "autoRegisterTemplateVersion": "ESv7", + "registerTemplateFailure": "IndexAnyway" + } + } ], - "Enrich": ["FromLogContext", "WithMachineName"], + "Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithProcessName"], "Properties": { - "Appname": "FeatureWerkSearchAPI", - "Environment": "Development" + "Appname": "TxSearchApi", + "Environment": "Development" } } } diff --git a/SearchWebApi/appsettings.json b/SearchWebApi/appsettings.json index 2780079..bac3776 100644 --- a/SearchWebApi/appsettings.json +++ b/SearchWebApi/appsettings.json @@ -1,9 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft": "Information", - "Microsoft.Hosting.Lifetime": "Information" + "Default": "Information" } }, "ElasticApm": @@ -14,5 +12,31 @@ "Environment": "Development", "CaptureBody": "all" }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Serilog": { + "MinimumLevel": { + "Default": "Information", + "Override": { + "System": "Information", + "Microsoft": "Information" + } + }, + "WriteTo": [ + { + "Name": "Elasticsearch", + "Args": { + "nodeUris": "http://192.168.178.44:9200", + "indexFormat": "TextorSearchLoggingIndex-{0:yyyy.MM}", + "autoRegisterTemplate": true, + "autoRegisterTemplateVersion": "ESv7", + "registerTemplateFailure": "IndexAnyway" + } + } + ], + "Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithProcessName"], + "Properties": { + "Appname": "TxSearchApi", + "Environment": "Production" + } + } }