Skip to content

Commit

Permalink
Added ELK Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
umerfaruk committed Aug 17, 2024
1 parent ac0ba6a commit cd292d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 51 deletions.
8 changes: 2 additions & 6 deletions src/Inshapardaz.Api/Inshapardaz.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elastic.Serilog.Sinks" Version="8.11.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-beta.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="Paramore.Brighter.Extensions.DependencyInjection" Version="9.7.8" />
<PackageReference Include="Paramore.Darker.AspNetCore" Version="4.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
77 changes: 32 additions & 45 deletions src/Inshapardaz.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using Microsoft.Net.Http.Headers;
using System.Text.Json.Serialization;
using System.Text.Json;
using Elastic.Channels;
using Elastic.Ingest.Elasticsearch;
using Elastic.Serilog.Sinks;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Inshapardaz.Adapter.Ocr.Google;
using Inshapardaz.Api.Helpers;
Expand All @@ -21,53 +24,14 @@
using Inshapardaz.Api.Infrastructure.Middleware;
using Inshapardaz.Domain.Ports.Query.Library;
using Microsoft.AspNetCore.Http.Features;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using Serilog;
using Serilog.Formatting.Compact;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel(o => o.Limits.MaxRequestBodySize = null);

//=====================================================================
// Configure open telemetry

const string serviceName = "Inshapardaz";

string tracingOtlpEndpoint = builder.Configuration["OLTP_ENDPOINT_URL"];

if (!builder.Environment.IsDevelopment())
{
builder.Logging.AddOpenTelemetry(options => options
.SetResourceBuilder(ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddConsoleExporter());

builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddMeter("Microsoft.AspnetCore.Hosting")
.AddMeter("Microsoft.AspnetCore.Server.Kestrel")
.AddMeter("System.Net.Http")
.AddPrometheusExporter()
.AddConsoleExporter())
.WithTracing(tracing =>
{
tracing
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();
if (tracingOtlpEndpoint != null)
{
tracing.AddOtlpExporter(opt => opt.Endpoint = new Uri(tracingOtlpEndpoint));
}
else
{
tracing.AddConsoleExporter();
}
});
}

//=====================================================================
// Add services to the container.

Expand All @@ -76,6 +40,32 @@
var configSection = builder.Configuration.GetSection("AppSettings");
builder.Services.Configure<Settings>(configSection);

builder.Host.UseSerilog((ctx, cfg) =>
{
var config = cfg.Enrich.WithProperty("Application", serviceName)
.Enrich.WithProperty("Environment", ctx.HostingEnvironment.EnvironmentName)
.WriteTo.Console(new RenderedCompactJsonFormatter());
if (!string.IsNullOrEmpty(ctx.Configuration["elk"]))
{
config.WriteTo.Elasticsearch(new[] { new Uri(ctx.Configuration["elk"]) }, opts =>
{
opts.BootstrapMethod = BootstrapMethod.Failure;
opts.ConfigureChannel = channelOpts =>
{
channelOpts.BufferOptions = new BufferOptions
{
ExportMaxConcurrency = 10
};
};
}, transport =>
{
// transport.Authentication(new BasicAuthentication(username, password)); // Basic Auth
// transport.Authentication(new ApiKey(base64EncodedApiKey)); // ApiKey
});
}
});

//--------------------------------------------------------------------
builder.Services.AddCors(options =>
{
Expand Down Expand Up @@ -187,10 +177,6 @@
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.MapPrometheusScrapingEndpoint();
}

app.UseCors(x => x
.SetIsOriginAllowed(origin => true)
Expand All @@ -203,6 +189,7 @@

app.UseAuthorization();
app.UseRequestLogging();
app.UseSerilogRequestLogging();
app.UseMiddleware<ErrorHandlerMiddleware>();
app.UseMiddleware<LibraryConfigurationMiddleware>();
app.UseStatusCodeMiddleWare();
Expand Down

0 comments on commit cd292d5

Please sign in to comment.