-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #388 from DFE-Digital/feature/add-serilog-api-user…
…-enricher Feature/add serilog api user enricher
- Loading branch information
Showing
5 changed files
with
117 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
using System; | ||
using TramsDataApi; | ||
using TramsDataApi.SerilogCustomEnrichers; | ||
|
||
namespace TramsDataApi | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
|
||
var startup = new Startup(builder.Configuration); | ||
|
||
startup.ConfigureServices(builder.Services); | ||
|
||
builder.Host.UseSerilog((context, services, loggerConfiguration) => | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) | ||
{ | ||
var builder = Host.CreateDefaultBuilder(args); | ||
return builder.ConfigureLogging(c => | ||
{ | ||
c.ClearProviders(); | ||
c.AddConsole(); | ||
}) | ||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); | ||
} | ||
} | ||
} | ||
var enricher = services.GetRequiredService<ApiUserEnricher>(); | ||
|
||
loggerConfiguration | ||
.WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces) | ||
.Enrich.FromLogContext() | ||
.Enrich.With(enricher) | ||
.WriteTo.Console(); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>(); | ||
|
||
startup.Configure(app, app.Environment, provider); | ||
|
||
ILogger<Program> logger = app.Services.GetRequiredService<ILogger<Program>>(); | ||
|
||
logger.LogInformation("Logger is working..."); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using TramsDataApi.ResponseModels; | ||
using TramsDataApi.UseCases; | ||
|
||
namespace TramsDataApi.SerilogCustomEnrichers | ||
{ | ||
public class ApiUserEnricher : ILogEventEnricher | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IUseCase<string, ApiUser> _apiKeyService; | ||
|
||
public ApiUserEnricher(IHttpContextAccessor httpContextAccessor, IUseCase<string, ApiUser> apiKeyService) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_apiKeyService = apiKeyService; | ||
} | ||
|
||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
var httpContext = _httpContextAccessor.HttpContext; | ||
|
||
if (httpContext is null) | ||
{ | ||
return; | ||
} | ||
|
||
ApiUser user = null; | ||
|
||
if (httpContext.Request.Headers.TryGetValue("ApiKey", out var apiKey)) | ||
{ | ||
user = _apiKeyService.Execute(apiKey); | ||
} | ||
|
||
var httpContextModel = new HttpContextModel | ||
{ | ||
Method = httpContext.Request.Method, | ||
User = user?.UserName ?? "Unknow or not applicable" | ||
|
||
}; | ||
|
||
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("ApiUser", httpContextModel.User, true)); | ||
} | ||
} | ||
|
||
public class HttpContextModel | ||
{ | ||
public string Method { get; init; } | ||
|
||
public string User { get; init; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters