From a1d508b14af72bc83288465923b36b7651909567 Mon Sep 17 00:00:00 2001 From: cihataydin Date: Mon, 19 May 2025 16:45:22 +0300 Subject: [PATCH 1/2] feat(41): enables database query logging with serilog - adds sample controler for test --- src/Api/Controllers/V1/SampleController.cs | 51 ++++++++++++++++++++++ src/Infra/Data/DataContext.cs | 9 ++++ src/Infra/Infra.csproj | 9 ++++ 3 files changed, 69 insertions(+) create mode 100644 src/Api/Controllers/V1/SampleController.cs diff --git a/src/Api/Controllers/V1/SampleController.cs b/src/Api/Controllers/V1/SampleController.cs new file mode 100644 index 0000000..99ec6bc --- /dev/null +++ b/src/Api/Controllers/V1/SampleController.cs @@ -0,0 +1,51 @@ +namespace Api.Controllers.V1 +{ + using Asp.Versioning; + using Domain.Entities; + using Domain.Interfaces; + using Microsoft.AspNetCore.Mvc; + + /// + /// Represents the sample controller. + /// + [ApiController] + [Route("sample")] + [Tags("sample")] + [ApiVersion("1.0")] + public class SampleController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching", + }; + + private readonly IRepository repository; + + /// + /// Initializes a new instance of the class. + /// + /// The repository. + public SampleController(IRepository repository) + { + this.repository = repository; + } + + /// + /// Gets the weather forecast. + /// + /// The weather forecast. + [HttpGet] + public async Task> Get() + { + this.repository.Add(new SampleEntity { Name = "Sample-1" }); + var result = await this.repository.ListAsync(); + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + Summaries[Random.Shared.Next(Summaries.Length)])) + .ToArray(); + return forecast; + } + } +} diff --git a/src/Infra/Data/DataContext.cs b/src/Infra/Data/DataContext.cs index eced47b..8d344ba 100644 --- a/src/Infra/Data/DataContext.cs +++ b/src/Infra/Data/DataContext.cs @@ -1,10 +1,19 @@ using Domain.Entities; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Serilog; + namespace Infra.Data; public class DataContext : DbContext { public DataContext(DbContextOptions opts) : base(opts) { } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.LogTo(Log.Logger.Information); + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Products => Set(); } \ No newline at end of file diff --git a/src/Infra/Infra.csproj b/src/Infra/Infra.csproj index 9806bce..8a6bc7c 100644 --- a/src/Infra/Infra.csproj +++ b/src/Infra/Infra.csproj @@ -7,11 +7,20 @@ + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all + \ No newline at end of file From 7ec1492dfa74d41f6a9020bccc818622245c6589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Ayd=C4=B1n?= <44273782+cihataydin@users.noreply.github.com> Date: Mon, 19 May 2025 16:51:30 +0300 Subject: [PATCH 2/2] refactor(42): considers copilot review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Infra/Data/DataContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infra/Data/DataContext.cs b/src/Infra/Data/DataContext.cs index 8d344ba..53e89cd 100644 --- a/src/Infra/Data/DataContext.cs +++ b/src/Infra/Data/DataContext.cs @@ -11,7 +11,7 @@ public DataContext(DbContextOptions opts) : base(opts) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.LogTo(Log.Logger.Information); + optionsBuilder.LogTo(message => Log.Information(message)); base.OnConfiguring(optionsBuilder); }