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",
"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,

View File

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

View File

@@ -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<EFBFBD>gung gestellt von {nameof(GetAllLookups)}");
_logger.LogInformation($"Lookups zur Verfügung gestellt von {nameof(GetAllLookups)}");
return new JsonResult(result);
}

View File

@@ -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<Startup>();
webBuilder.UseSerilog();
webBuilder.UseConfiguration(Configuration);
webBuilder.SuppressStatusMessages(true);
})
.ConfigureLogging((context, builder) =>
{
builder.ClearProviders();
builder.AddConsole();
builder.AddSerilog();
builder.AddOpenTelemetry(options =>
{

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
</PropertyGroup>
@@ -17,7 +17,13 @@
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" 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.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.ElasticSearch" Version="8.4.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.2" />

View File

@@ -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<IFwMariaSearchRepo, FwMariaSearchRepo>();
services.AddTransient<ISearchLogic, SearchLogic.SearchLogic>();
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");

View File

@@ -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",
{
"Name": "Elasticsearch",
"Args": {
"path": "/tmp/FwSearchApi.log",
"rollingInterval": "Day"
"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"
}
}
}

View File

@@ -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"
}
}
}