Skip to content
Snippets Groups Projects
Commit 074a6e32 authored by Nikoline Vistoft Bartholin's avatar Nikoline Vistoft Bartholin
Browse files

Changes in logging middleware

parent 976a6013
No related branches found
No related tags found
No related merge requests found
using System.Diagnostics;
using assignment2.Interfaces;
using assignment2.Models;
public sealed class LoggerMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private readonly ILogService _logService;
public LoggerMiddleware(RequestDelegate next, ILogger<LoggerMiddleware> logger)
public LoggerMiddleware(RequestDelegate next, ILogger<LoggerMiddleware> logger, ILogService logService)
{
_next = next;
_logger = logger;
_logService = logService;
}
public async Task Invoke(HttpContext context)
......@@ -22,13 +26,14 @@ public sealed class LoggerMiddleware
// Add other details as needed
};
// Optionally, add user info if available.
if (context.User?.Identity?.IsAuthenticated == true)
var newLog = new Log
{
requestDetails["UserName"] = context.User?.Identity?.Name;
}
User = context.User?.Identity?.Name ?? "Anonymous",
Operation = context.Request.Method,
Timestamp = DateTime.UtcNow
};
_logger.LogInformation("Request completed with details: {@RequestDetails}", requestDetails);
_logger.LogInformation("Request completed with details: {@newLog}", newLog);
await _next(context);
}
}
\ No newline at end of file
......@@ -22,7 +22,7 @@ builder.Services.AddTransient<IExperienceService, ExperienceService>();
builder.Services.AddTransient<IProviderService, ProviderService>();
builder.Services.AddTransient<ISharedExperienceService, SharedExperienceService>();
builder.Services.AddTransient<IGuestService, GuestService>();
builder.Services.AddScoped<ILogService, LogsService>();
builder.Services.AddSingleton<ILogService, LogsService>();
Env.Load();
builder.Services.AddDbContext<ExperienceDBContext>();
builder.Services.AddFusionCache().WithDefaultEntryOptions(new FusionCacheEntryOptions
......
{
"BookStoreDatabase": {
"LoggingDB": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "AssignmentLoggingDB",
"LogsCollectionName": "Logs"
......
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