Skip to content

Commit

Permalink
Logger added.
Browse files Browse the repository at this point in the history
  • Loading branch information
selimaytac committed Aug 10, 2022
1 parent bf00efd commit efe8cca
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 1 deletion.
3 changes: 3 additions & 0 deletions DbContexts/PITDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace DTPersonalInfoTracker.DbContexts;
public class PITDbContext : DbContext
{
public DbSet<PersonelModel> Personels { get; set; }
public DbSet<LogModel> Logs { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand All @@ -16,5 +17,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonelModel>().HasKey(p => p.RecordId);
modelBuilder.Entity<LogModel>().HasKey(p => p.Id);
modelBuilder.Entity<LogModel>().Property(p => p.Id).ValueGeneratedOnAdd();
}
}
2 changes: 2 additions & 0 deletions Helpers/DbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static void AddFieldListsToDb(IList<PersonelModel> personelModels)
context.Personels.AddRange(personelModels);
context.SaveChanges();
LoggerHelper.Information("Personel list successfully added to database.");
LoggerHelper.LogToDb(context, new LogModel {Date = DateTime.Now, OperationName = "Update", Message = "Personel list successfully added to database"});
}

public static void InitializeDb(PITDbContext context)
Expand All @@ -60,5 +61,6 @@ public static void TruncateTable(PITDbContext context)
{
context.Personels.RemoveRange(context.Personels);
LoggerHelper.Information("Personel table truncated.");
LoggerHelper.LogToDb(context, new LogModel {Date = DateTime.Now, OperationName = "Truncate", Message = "Personel table truncated."});
}
}
10 changes: 9 additions & 1 deletion Helpers/LoggerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Serilog;
using DTPersonalInfoTracker.DbContexts;
using DTPersonalInfoTracker.Models;
using Serilog;
using Serilog.Core;

namespace DTPersonalInfoTracker.Helpers;
Expand Down Expand Up @@ -33,4 +35,10 @@ public static void Information(string message)
{
DTLogger.Information(message);
}

public static void LogToDb(PITDbContext context, LogModel log)
{
context.Logs.Add(log);
context.SaveChanges();
}
}
96 changes: 96 additions & 0 deletions Migrations/20220810062226_LogLayerAdded.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Migrations/20220810062226_LogLayerAdded.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DTPersonalInfoTracker.Migrations
{
public partial class LogLayerAdded : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Logs",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Message = table.Column<string>(type: "nvarchar(max)", nullable: false),
OperationName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Date = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Logs", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Logs");
}
}
}
25 changes: 25 additions & 0 deletions Migrations/PITDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// <auto-generated />
using System;
using DTPersonalInfoTracker.DbContexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand All @@ -21,6 +22,30 @@ protected override void BuildModel(ModelBuilder modelBuilder)

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

modelBuilder.Entity("DTPersonalInfoTracker.Models.LogModel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

b.Property<DateTime>("Date")
.HasColumnType("datetime2");

b.Property<string>("Message")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("OperationName")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Logs");
});

modelBuilder.Entity("DTPersonalInfoTracker.Models.PersonelModel", b =>
{
b.Property<string>("RecordId")
Expand Down
9 changes: 9 additions & 0 deletions Models/LogModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DTPersonalInfoTracker.Models;

public class LogModel
{
public int Id { get; set; }
public string Message { get; set; }
public string OperationName { get; set; }
public DateTime Date { get; set; }
}

0 comments on commit efe8cca

Please sign in to comment.