OP and Logging to Elasticsearch

This commit is contained in:
Roland Fieger
2021-10-12 16:42:24 +02:00
parent 66f8d19f67
commit e9d30aad16
8 changed files with 81 additions and 41 deletions

2
.vscode/launch.json vendored
View File

@@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "build", "preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path. // 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": [], "args": [],
"cwd": "${workspaceFolder}/SearchWebApi", "cwd": "${workspaceFolder}/SearchWebApi",
"stopAtEntry": false, "stopAtEntry": false,

View File

@@ -27,6 +27,7 @@ namespace BusinessLogic.LookupLogic
public async Task<DataResult<List<LookupCategory>>> GetAllLookups() public async Task<DataResult<List<LookupCategory>>> GetAllLookups()
{ {
var result = await _lookupRepo.GetAllLookups(); var result = await _lookupRepo.GetAllLookups();
_logger.LogInformation($"Call to GetAllLookups ended with {nameof(result.GeneralResult)}");
return result; return result;
} }

View File

@@ -29,7 +29,7 @@ namespace FwSearchApi.Controllers
if (result.GeneralResult != Datamodels.BusinessModels.GeneralResults.Success) { if (result.GeneralResult != Datamodels.BusinessModels.GeneralResults.Success) {
throw new System.Exception("das war wohl nix"); throw new System.Exception("das war wohl nix");
} }
_logger.LogInformation($"Lookups zur Verf<EFBFBD>gung gestellt von {nameof(GetAllLookups)}"); _logger.LogInformation($"Lookups zur Verfügung gestellt von {nameof(GetAllLookups)}");
return new JsonResult(result); return new JsonResult(result);
} }

View File

@@ -24,7 +24,17 @@ namespace FwSearchApi
public static void Main(string[] args) 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) => public static IHostBuilder CreateHostBuilder(string[] args) =>
@@ -32,12 +42,15 @@ namespace FwSearchApi
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
webBuilder.UseSerilog();
webBuilder.UseConfiguration(Configuration); webBuilder.UseConfiguration(Configuration);
webBuilder.SuppressStatusMessages(true);
}) })
.ConfigureLogging((context, builder) => .ConfigureLogging((context, builder) =>
{ {
builder.ClearProviders(); builder.ClearProviders();
builder.AddConsole(); builder.AddConsole();
builder.AddSerilog();
builder.AddOpenTelemetry(options => builder.AddOpenTelemetry(options =>
{ {

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion> <LangVersion>10</LangVersion>
</PropertyGroup> </PropertyGroup>
@@ -17,7 +17,13 @@
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc7" /> <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc7" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc7" /> <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc7" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" /> <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.ExceptionStackTraceHash" Version="1.3.0" />
<PackageReference Include="Serilog.Exceptions" Version="7.1.0" />
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="8.4.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="serilog.sinks.console" Version="4.0.0" /> <PackageReference Include="serilog.sinks.console" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.ElasticSearch" Version="8.4.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.2" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.2" /> <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.2" /> <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.2" />

View File

@@ -1,27 +1,22 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Repositories;
using Repositories.Interfaces;
using BusinessLogic.LookupLogic; using BusinessLogic.LookupLogic;
using BusinessLogic.LookupLogic.Interfaces; using BusinessLogic.LookupLogic.Interfaces;
using SearchLogic;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Resources; using OpenTelemetry.Resources;
using OpenTelemetry.Trace; using OpenTelemetry.Trace;
using Elastic.Apm.AspNetCore;
using Elastic.Apm.EntityFrameworkCore; using Repositories;
using Elastic.Apm; using Repositories.Interfaces;
using SearchLogic;
namespace FwSearchApi namespace FwSearchApi
{ {
@@ -50,13 +45,12 @@ namespace FwSearchApi
services.AddTransient<IFwMariaSearchRepo, FwMariaSearchRepo>(); services.AddTransient<IFwMariaSearchRepo, FwMariaSearchRepo>();
services.AddTransient<ISearchLogic, SearchLogic.SearchLogic>(); services.AddTransient<ISearchLogic, SearchLogic.SearchLogic>();
services.AddSwaggerGen(); services.AddSwaggerGen();
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); //needed if no Security is used when calling APM Server
services.AddOpenTelemetryTracing((builder) => builder services.AddOpenTelemetryTracing((builder) => builder
//.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("TxSearchApi")) .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("TxSearchApi"))
.AddAspNetCoreInstrumentation() .AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation() .AddHttpClientInstrumentation()
.AddEntityFrameworkCoreInstrumentation() .AddEntityFrameworkCoreInstrumentation(o => o.SetDbStatementForText = true) //logging of Statement as Span
.AddConsoleExporter()
.AddOtlpExporter(otlpOptions => { .AddOtlpExporter(otlpOptions => {
otlpOptions.Endpoint = new Uri("http://192.168.178.44:8200"); otlpOptions.Endpoint = new Uri("http://192.168.178.44:8200");
})); }));
@@ -75,8 +69,6 @@ namespace FwSearchApi
app.UseSwagger(); app.UseSwagger();
//app.UseElasticApm(_configuration, new EfCoreDiagnosticsSubscriber());
app.UseSwaggerUI(c => app.UseSwaggerUI(c =>
{ {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FeatureWerk Search API V1"); c.SwaggerEndpoint("/swagger/v1/swagger.json", "FeatureWerk Search API V1");

View File

@@ -1,8 +1,8 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Warning", "Default": "Information",
"Microsoft": "Warning", "Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
}, },
@@ -17,24 +17,28 @@
}, },
"Serilog": { "Serilog": {
"MinimumLevel": { "MinimumLevel": {
"Default": "Information", "Default": "Information",
"Override": { "Override": {
"System": "Information", "System": "Information",
"Microsoft": "Warning" "Microsoft": "Information"
} }
}, },
"WriteTo": [ "WriteTo": [
{ "Name": "File", {
"Name": "Elasticsearch",
"Args": { "Args": {
"path": "/tmp/FwSearchApi.log", "nodeUris": "http://192.168.178.44:9200",
"rollingInterval": "Day" "indexFormat": "TextorSearchLoggingIndex-{0:yyyy.MM}",
"autoRegisterTemplate": true,
"autoRegisterTemplateVersion": "ESv7",
"registerTemplateFailure": "IndexAnyway"
} }
} }
], ],
"Enrich": ["FromLogContext", "WithMachineName"], "Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithProcessName"],
"Properties": { "Properties": {
"Appname": "FeatureWerkSearchAPI", "Appname": "TxSearchApi",
"Environment": "Development" "Environment": "Development"
} }
} }
} }

View File

@@ -1,9 +1,7 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information"
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
} }
}, },
"ElasticApm": "ElasticApm":
@@ -14,5 +12,31 @@
"Environment": "Development", "Environment": "Development",
"CaptureBody": "all" "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"
}
}
} }