Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • bad1/assignment2
1 result
Show changes
Commits on Source (4)
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
......@@ -2,6 +2,8 @@ using assignment1.Interfaces;
using assignment1.Services;
using DotNetEnv;
using assignment2.Services;
using Serilog;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
......@@ -15,6 +17,9 @@ builder.Services.AddTransient<IGuestService, GuestService>();
Env.Load();
builder.Services.AddDbContext<ExperienceDBContext>();
// Adds logger using appsettings.json for configuration
builder.Services.AddSerilog(options => options.ReadFrom.Configuration(builder.Configuration));
// Enable Swagger/OpenAPI
builder.Services.AddEndpointsApiExplorer();
......@@ -50,6 +55,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" />
</ItemGroup>
......