From 343487bba52208ff18f24745f7a5f4ea0530bb0b Mon Sep 17 00:00:00 2001 From: BloodShop Date: Mon, 21 Nov 2022 09:59:25 +0200 Subject: [PATCH] _logger + testing --- Controllers/AccountController.cs | 16 ++++++++++++++-- Controllers/HomeController.cs | 13 ++++++++++++- PetShopProj.csproj | 1 + PetShopProj.sln | 8 +++++++- Program.cs | 4 ++++ Views/Home/Animal.cshtml | 2 +- appsettings.json | 18 +++++++++++++++++- log.txt | 2 ++ 8 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 log.txt diff --git a/Controllers/AccountController.cs b/Controllers/AccountController.cs index 539dfa6..6aea40e 100644 --- a/Controllers/AccountController.cs +++ b/Controllers/AccountController.cs @@ -10,14 +10,17 @@ public class AccountController : Controller readonly UserManager _userManager; readonly SignInManager _signInManager; readonly RoleManager _roleManager; + readonly ILogger _logger; public AccountController(UserManager userManager, SignInManager signInManager, - RoleManager roleManager) + RoleManager roleManager, + ILogger logger) { _userManager = userManager; _signInManager = signInManager; _roleManager = roleManager; + _logger = logger; } [HttpPost] @@ -25,6 +28,7 @@ public AccountController(UserManager userManager, public async Task Logout() { await _signInManager.SignOutAsync(); + _logger.LogInformation($"An account has logged out !!"); return RedirectToAction("Index", "Home"); } @@ -39,8 +43,12 @@ public async Task Login(LoginViewModel model) var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, false); if (result.Succeeded) + { + _logger.LogInformation($"{model.UserName} has logged in !!"); return RedirectToAction("Index", "Home"); - + } + + _logger.LogInformation($"!! Invalid Login Attempt username: {model.UserName}, pass:{model.Password}"); ModelState.AddModelError(string.Empty, "Invalid Login Attempt"); } return View(model); @@ -60,11 +68,15 @@ public async Task Register(RegisterViewModel model) if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent: false); + _logger.LogInformation($"New registration - username: {model.UserName}, pass: {model.Password}"); return RedirectToAction("Index", "Home"); } foreach (var error in result.Errors) + { + _logger.LogError("!!" + error.Description); ModelState.AddModelError(string.Empty, error.Description); + } } return View(model); } diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index fad8fe7..2c762ec 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -11,11 +11,13 @@ namespace PetShopProj.Controllers { public class HomeController : Controller { + readonly ILogger _logger; readonly IRepository _repo; readonly IWebHostEnvironment _hostingEnvironment; - public HomeController(IRepository repository, IWebHostEnvironment hostingEnvironment) + public HomeController(IRepository repository, IWebHostEnvironment hostingEnvironment, ILogger logger) { + _logger = logger; _repo = repository; _hostingEnvironment = hostingEnvironment; } @@ -54,7 +56,10 @@ public IActionResult Search(string text) public IActionResult Animal(int id, string comment) { if (comment != null && comment != string.Empty) + { _repo.AddComment(id, comment); + _logger.LogInformation("!! Attempted to add an empty comment"); + } return View(_repo.GetAnimal(id)); } @@ -85,6 +90,7 @@ public IActionResult AddAnimal(AddAnimalViewModel model) return RedirectToAction(nameof(Animal), new { id = newAnimal.Id }); } + _logger.LogError("!! Attempted to add an invalid image"); return View("InvalidImageError", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)); } @@ -134,6 +140,7 @@ public IActionResult EditAnimal(EditAnimalViewModel model) return RedirectToAction(nameof(Animal), new { id = animal.Id }); } + _logger.LogError("!! Attempted to add an invalid image"); return View("InvalidImageError", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)); } @@ -180,7 +187,10 @@ public IActionResult ManageCategories(int id) var category = _repo.GetCategoryById(id)!; if (category.Animals!.Count != 0) + { + _logger.LogError("!! Attempted to remove category with animals."); return View("RemoveCategoryError"); + } _repo.DeleteCategory(id); return View(_repo.GetCategory()); @@ -200,6 +210,7 @@ public IActionResult AddCategory(AddCategoryViewModel model) var newCategory = new Category { Name = model.Name }; _repo.AddCategory(newCategory); + _logger.LogInformation("A category has been added."); return RedirectToAction(nameof(ManageCategories)); } return View(); diff --git a/PetShopProj.csproj b/PetShopProj.csproj index 4b96a6c..b30929c 100644 --- a/PetShopProj.csproj +++ b/PetShopProj.csproj @@ -21,6 +21,7 @@ + diff --git a/PetShopProj.sln b/PetShopProj.sln index b35b8ef..9ea2b66 100644 --- a/PetShopProj.sln +++ b/PetShopProj.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PetShopProj", "PetShopProj.csproj", "{1E0C46F0-F547-4C88-B39E-BC8E002A4FA0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PetShopProj", "PetShopProj.csproj", "{1E0C46F0-F547-4C88-B39E-BC8E002A4FA0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PetShopProj.Tests", "..\PetShopProj.Tests\PetShopProj.Tests.csproj", "{F19177C5-9D5B-4C1C-9833-FE3BD1571C8E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {1E0C46F0-F547-4C88-B39E-BC8E002A4FA0}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E0C46F0-F547-4C88-B39E-BC8E002A4FA0}.Release|Any CPU.ActiveCfg = Release|Any CPU {1E0C46F0-F547-4C88-B39E-BC8E002A4FA0}.Release|Any CPU.Build.0 = Release|Any CPU + {F19177C5-9D5B-4C1C-9833-FE3BD1571C8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F19177C5-9D5B-4C1C-9833-FE3BD1571C8E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F19177C5-9D5B-4C1C-9833-FE3BD1571C8E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F19177C5-9D5B-4C1C-9833-FE3BD1571C8E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Program.cs b/Program.cs index d1c5083..2ebc74a 100644 --- a/Program.cs +++ b/Program.cs @@ -2,11 +2,15 @@ using Microsoft.EntityFrameworkCore; using PetShopProj.Data; using PetShopProj.Repositories; +using Serilog; var builder = WebApplication.CreateBuilder(args); builder.Services.AddTransient(); +builder.Host.UseSerilog((ctx, lc) => + lc.ReadFrom.Configuration(ctx.Configuration)); string connectionString = builder.Configuration["ConnectionStrings:DefaultConnection"]; builder.Services.AddDatabaseDeveloperPageExceptionFilter(); + builder.Services.AddDbContext(options => options.UseLazyLoadingProxies().UseSqlServer(connectionString)); builder.Services.AddIdentity(options => diff --git a/Views/Home/Animal.cshtml b/Views/Home/Animal.cshtml index 4d6973e..6ef0151 100644 --- a/Views/Home/Animal.cshtml +++ b/Views/Home/Animal.cshtml @@ -27,6 +27,6 @@ @*asp-route-comment=""*@ - + @* Buy *@ diff --git a/appsettings.json b/appsettings.json index b10661d..7c8c6be 100644 --- a/appsettings.json +++ b/appsettings.json @@ -8,5 +8,21 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Serilog": { + "MinimumLevel": { + "Default": "Warning" + }, + "WriteTo": [ + { + "Name": "Console" + }, + { + "Name": "File", + "Args": { + "path": "./log.txt" + } + } + ] + } } \ No newline at end of file diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..938059d --- /dev/null +++ b/log.txt @@ -0,0 +1,2 @@ +2022-11-21 09:58:19.720 +02:00 [WRN] Failed to determine the https port for redirect. +2022-11-21 09:58:36.490 +02:00 [ERR] !! Attempted to remove category with animals.