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 (2)
......@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using assignment2.Interfaces;
using assignment2.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
......@@ -12,8 +13,8 @@ namespace assignment2.Controllers
[ApiController]
public class LogController : ControllerBase
{
private readonly LogsService _logsService;
public LogController(LogsService logsService) => _logsService = logsService;
private readonly ILogService _logsService;
public LogController(ILogService logsService) => _logsService = logsService;
[HttpGet]
public async Task<IActionResult> GetAsync(string? user, string? operation, DateTime? startDate, DateTime? endTime)
......
......@@ -9,6 +9,5 @@ namespace assignment2.Interfaces
public interface ILogService
{
Task<IEnumerable<Log>> GetAsync(string? user, string? operation, DateTime? startDate, DateTime? endTime);
}
}
\ No newline at end of file
......@@ -17,23 +17,23 @@ public sealed class LoggerMiddleware
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
};
//// 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
//};
var newLog = new Log
var log = new Log
{
User = context.User?.Identity?.Name ?? "Anonymous",
Operation = context.Request.Method,
Timestamp = DateTime.UtcNow
};
_logger.LogInformation("Request completed with details: {@newLog}", newLog);
//_logger.LogInformation("Request completed with details: {@Log}", log);
await _next(context);
}
}
\ No newline at end of file
......@@ -7,19 +7,20 @@ using MongoDB.Bson.Serialization.Attributes;
namespace assignment2.Models
{
[BsonIgnoreExtraElements]
public class Log
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
[BsonElement("api_user")]
//[BsonElement("api_user")]
public string User { get; set; } = string.Empty;
[BsonElement("operation")]
//[BsonElement("operation")]
public string Operation { get; set; } = string.Empty;
[BsonElement("timestamp")]
//[BsonElement("UtcTimeStamp")]
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}
}
\ No newline at end of file
using assignment2.Models;
using MongoDB.Bson.Serialization.Attributes;
namespace assignment1.Models
{
[BsonIgnoreExtraElements]
public class SeriLogModel
{
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string? Id { get; set; }
public Log Log { get; set; } = new Log();
}
}
......@@ -8,6 +8,7 @@ using Serilog;
using Microsoft.Extensions.Options;
using assignment2.Models;
using assignment2.Interfaces;
using MongoDB.Bson;
var builder = WebApplication.CreateBuilder(args);
......@@ -70,7 +71,7 @@ app.UseSwaggerUI(c =>
c.RoutePrefix = ""; // Swagger loads at the root URL
});
//app.UseSerilogRequestLogging();
//app.UseSerilogRequestLogging
app.UseMiddleware<LoggerMiddleware>();
// Conditionally handle HTTPS redirection based on environment
......
......@@ -5,6 +5,8 @@ using System.Threading.Tasks;
using assignment2.Interfaces;
using assignment2.Models;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace assignment2.Services
......@@ -12,18 +14,29 @@ namespace assignment2.Services
public class LogsService : ILogService
{
private readonly IMongoCollection<Log> _logsCollection;
private readonly MongoClient _mongoClient;
private readonly IMongoDatabase _database;
public LogsService(IOptions<LogsDatabaseSettings> logsDatabaseSettings)
{
var mongoClient = new MongoClient(
_mongoClient = new MongoClient(
logsDatabaseSettings.Value.ConnectionString);
var mongoDatabase = mongoClient.GetDatabase(
_database = _mongoClient.GetDatabase(
logsDatabaseSettings.Value.DatabaseName);
_logsCollection = mongoDatabase.GetCollection<Log>(
//BsonClassMap.RegisterClassMap<Log>(cm =>
//{
// cm.AutoMap();
// cm.MapMember(c => c.User).SetElementName("Properties.Log.User");
// cm.MapMember(c => c.Operation).SetElementName("Properties.Log.Operation");
// cm.MapMember(c => c.Timestamp).SetElementName("Properties.Log.Timestamp");
// cm.SetIgnoreExtraElements(true);
//});
_logsCollection = _database.GetCollection<Log>(
logsDatabaseSettings.Value.LogsCollectionName);
}
......