From d4eef3da9adf94a16ebfde598d3d120a2486be47 Mon Sep 17 00:00:00 2001 From: tonyfages Date: Fri, 15 Mar 2024 17:09:39 +0100 Subject: [PATCH] fix logger --- Verax_API_EF/Verax_API_EF/API/log.txt | 17 +++++++ .../DbContextLib/LibraryContext.cs | 49 ++++++++----------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/Verax_API_EF/Verax_API_EF/API/log.txt b/Verax_API_EF/Verax_API_EF/API/log.txt index 38b147c..7d66f59 100644 --- a/Verax_API_EF/Verax_API_EF/API/log.txt +++ b/Verax_API_EF/Verax_API_EF/API/log.txt @@ -15,3 +15,20 @@ info: 03/15/2024 16:59:30.063 RelationalEventId.CommandExecuted[20101] (Microsof Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" FROM "ArticleSet" AS "a" +info: 03/15/2024 17:08:29.557 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/15/2024 17:08:29.561 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/15/2024 17:08:29.567 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "MigrationId", "ProductVersion" + FROM "__EFMigrationsHistory" + ORDER BY "MigrationId"; +info: 03/15/2024 17:08:29.575 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations) + No migrations were applied. The database is already up to date. +info: 03/15/2024 17:08:33.305 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" + FROM "ArticleSet" AS "a" diff --git a/Verax_API_EF/Verax_API_EF/DbContextLib/LibraryContext.cs b/Verax_API_EF/Verax_API_EF/DbContextLib/LibraryContext.cs index b8032db..c62563b 100644 --- a/Verax_API_EF/Verax_API_EF/DbContextLib/LibraryContext.cs +++ b/Verax_API_EF/Verax_API_EF/DbContextLib/LibraryContext.cs @@ -6,27 +6,32 @@ namespace DbContextLib; public class LibraryContext : DbContext { - - private static readonly StreamWriter LogFile = new StreamWriter("log.txt"); + public LibraryContext() : base() - { } + { + } public LibraryContext(DbContextOptions options) : base(options) - { } - - - + { + } + + + public DbSet ArticleSet { get; set; } public DbSet UserSet { get; set; } public DbSet FormSet { get; set; } - + public DbSet ArticleUserSet { get; set; } - + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.LogTo(message => LogFile.WriteLine(message), LogLevel.Information); + optionsBuilder.LogTo(message => + { + using var logFile = new StreamWriter("log.txt", append: true); + logFile.WriteLine(message); + }, LogLevel.Information); if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlite($"Data Source=Entity_FrameWork.Article.db"); @@ -41,12 +46,12 @@ public class LibraryContext : DbContext .HasMany(a => a.Users) .WithMany(a => a.Articles) .UsingEntity(); - + modelBuilder.Entity() .HasMany(u => u.Forms) .WithOne(f => f.User) .HasForeignKey(f => f.UserEntityPseudo); - + modelBuilder.Entity() .HasOne(f => f.User) .WithMany(u => u.Forms) @@ -80,7 +85,7 @@ public class LibraryContext : DbContext Description = "M&M's new recipe is out and it's the best chocolate ever", DatePublished = "2022-02-06", LectureTime = 1, - Author = "M&M's Red" + Author = "M&M's Red" } ); @@ -107,7 +112,7 @@ public class LibraryContext : DbContext Nom = "Sillard", Prenom = "Noa", Pseudo = "NoaSil", Mail = "", Mdp = "1234", Role = "Admin" } ); - + modelBuilder.Entity().HasData( new ArticleUserEntity { @@ -135,11 +140,11 @@ public class LibraryContext : DbContext UserEntityPseudo = "TomS" } ); - + modelBuilder.Entity().HasData( new FormEntity { - Id = 1, + Id = 1, DatePublication = "Form 1 Description", Link = "hhtp://form1.com", UserEntityPseudo = "Sha" @@ -161,16 +166,4 @@ public class LibraryContext : DbContext ); */ } - - public override void Dispose() - { - base.Dispose(); - LogFile.Dispose(); - } - - public override async ValueTask DisposeAsync() - { - await base.DisposeAsync(); - await LogFile.DisposeAsync(); - } } \ No newline at end of file