Skip to content
Snippets Groups Projects
Commit 176f9595 authored by Hindubedu's avatar Hindubedu
Browse files

Merge branch 'main' of https://gitlab.au.dk/bad1/assignment2

parents fb3fd287 0ac900fe
No related branches found
No related tags found
No related merge requests found
File moved
using System.Diagnostics;
public sealed class LoggerMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public LoggerMiddleware(RequestDelegate next, ILogger<LoggerMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
// Create a dictionary or a custom object to hold request details.
var requestDetails = new Dictionary<string, object>
{
{ "RequestHost", context.Request.Host.ToString() },
{ "RequestScheme", context.Request.Scheme },
{ "RequestPath", context.Request.Path.ToString() }
// Add other details as needed
};
// Optionally, add user info if available.
if (context.User?.Identity?.IsAuthenticated == true)
{
requestDetails["UserName"] = context.User?.Identity?.Name;
}
_logger.LogInformation("Request completed with details: {@RequestDetails}", requestDetails);
await _next(context);
}
}
\ No newline at end of file
......@@ -5,6 +5,8 @@ using assignment2.Services;
using ZiggyCreatures.Caching.Fusion;
using Microsoft.Extensions.Caching.Memory;
using Serilog;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
......@@ -24,6 +26,9 @@ builder.Services.AddFusionCache().WithDefaultEntryOptions(new FusionCacheEntryOp
});
// Adds logger using appsettings.json for configuration
builder.Services.AddSerilog(options => options.ReadFrom.Configuration(builder.Configuration));
// Enable Swagger/OpenAPI
builder.Services.AddEndpointsApiExplorer();
......@@ -59,6 +64,9 @@ app.UseSwaggerUI(c =>
c.RoutePrefix = ""; // Swagger loads at the root URL
});
//app.UseSerilogRequestLogging();
app.UseMiddleware<LoggerMiddleware>();
// Conditionally handle HTTPS redirection based on environment
app.UseAuthorization();
app.MapControllers();
......
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "MongoDBBson",
"Args": {
"databaseUrl": "mongodb://localhost:27017/AssignmentLoggingDB?appName=MongoDB+Compass&directConnection=true&serverSelectionTimeoutMS=2000",
"collectionName": "logs",
"cappedMaxSizeMb": "50",
"cappedMaxDocuments": "1000"
}
}
// Add other sinks here if desired...
],
"Enrich": [
"FromLogContext"
]
},
"AllowedHosts": "*"
}
......@@ -18,6 +18,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.MongoDB" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.1.0" />
</ItemGroup>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment