From 220be1c9cdace210a70cbe17fc124d5998a36e6c Mon Sep 17 00:00:00 2001 From: Antoine Jourdain Date: Sun, 21 Jan 2024 18:15:31 +0100 Subject: [PATCH] Ajout des logs --- Project/adminBlazor/adminBlazor/Program.cs | 119 +++++++++--------- .../adminBlazor/Services/AuthService.cs | 11 ++ .../adminBlazor/Services/DataApiService.cs | 14 ++- .../adminBlazor/adminBlazor/appsettings.json | 17 ++- 4 files changed, 96 insertions(+), 65 deletions(-) diff --git a/Project/adminBlazor/adminBlazor/Program.cs b/Project/adminBlazor/adminBlazor/Program.cs index 62ec55b..333c656 100644 --- a/Project/adminBlazor/adminBlazor/Program.cs +++ b/Project/adminBlazor/adminBlazor/Program.cs @@ -13,6 +13,7 @@ using Microsoft.Extensions.Options; using Microsoft.Net.Http.Headers; using adminBlazor.Models; using adminBlazor; +using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Components.Authorization; @@ -22,15 +23,13 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddRazorPages(); - builder.Services.AddServerSideBlazor(); - builder.Services.AddSingleton(); +builder.Services.AddRazorPages(); +builder.Services.AddServerSideBlazor(); +builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); - - builder.Services.AddScoped(); builder.Services.AddOptions(); @@ -41,67 +40,67 @@ builder.Services.AddScoped(); builder.Services.AddHttpClient(); - builder.Services.AddBlazoredLocalStorage(); - builder.Services.AddBlazoredModal(); +builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddBlazoredModal(); // Add the controller of the app builder.Services.AddControllers(); - // Add the localization to the app and specify the resources path - builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); - - // Configure the localtization - builder.Services.Configure(options => - { - // Set the default culture of the web site - options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US")); - - // Declare the supported culture - options.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; - options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; - }); - - - builder.Services - .AddBlazorise() - .AddBootstrapProviders() - .AddFontAwesomeIcons(); - - - var app = builder.Build(); - - // Configure the HTTP request pipeline. - if (!app.Environment.IsDevelopment()) - { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } - - app.UseHttpsRedirection(); - - app.UseStaticFiles(); - - app.UseRouting(); - - // Get the current localization options - var options = ((IApplicationBuilder)app).ApplicationServices.GetService>(); - - if (options?.Value != null) - { - // use the default localization - app.UseRequestLocalization(options.Value); - } +// Add the localization to the app and specify the resources path +builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); + +// Configure the localization +builder.Services.Configure(options => +{ + // Set the default culture of the web site + options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US")); + + // Declare the supported culture + options.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; + options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; +}); + +builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); + + +builder.Services + .AddBlazorise() + .AddBootstrapProviders() + .AddFontAwesomeIcons(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (!app.Environment.IsDevelopment()) +{ + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); +} + +app.UseHttpsRedirection(); + +app.UseStaticFiles(); + +app.UseRouting(); + +// Get the current localization options +var options = ((IApplicationBuilder)app).ApplicationServices.GetService>(); + +if (options?.Value != null) +{ + // use the default localization + app.UseRequestLocalization(options.Value); +} // Add the controller to the endpoint - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - - app.MapBlazorHub(); - app.MapFallbackToPage("/_Host"); +app.UseEndpoints(endpoints => +{ + endpoints.MapControllers(); +}); - app.Run(); +app.MapBlazorHub(); +app.MapFallbackToPage("/_Host"); +app.Run(); \ No newline at end of file diff --git a/Project/adminBlazor/adminBlazor/Services/AuthService.cs b/Project/adminBlazor/adminBlazor/Services/AuthService.cs index 33053bb..e8ff894 100644 --- a/Project/adminBlazor/adminBlazor/Services/AuthService.cs +++ b/Project/adminBlazor/adminBlazor/Services/AuthService.cs @@ -9,6 +9,12 @@ public class AuthService : IAuthService { private static readonly List CurrentUser; + private readonly ILogger _logger; + + public AuthService(ILogger logger) + { + _logger = logger; + } static AuthService() { @@ -25,6 +31,7 @@ if (user == null) { + _logger.LogWarning("Authentication failed, field invalid."); throw new Exception("User name or password invalid !"); } @@ -45,13 +52,17 @@ if (user == null) { + _logger.LogWarning("Authentication : login failed, field invalid."); throw new Exception("User name or password invalid !"); } + + _logger.LogInformation("Authentication : login success."); } public void Register(RegisterRequest registerRequest) { CurrentUser.Add(new AppUser { UserName = registerRequest.UserName, Password = registerRequest.Password, Roles = new List { "guest" } }); + _logger.LogInformation("Authentication : register success."); } } } \ No newline at end of file diff --git a/Project/adminBlazor/adminBlazor/Services/DataApiService.cs b/Project/adminBlazor/adminBlazor/Services/DataApiService.cs index f8a9a68..910066e 100644 --- a/Project/adminBlazor/adminBlazor/Services/DataApiService.cs +++ b/Project/adminBlazor/adminBlazor/Services/DataApiService.cs @@ -8,11 +8,14 @@ namespace adminBlazor.Services public class DataApiService : IDataService { private readonly HttpClient _http; + private readonly ILogger _logger; public DataApiService( - HttpClient http) + HttpClient http, + ILogger logger) { _http = http; + _logger = logger; } public async Task Add(UserModel model) @@ -22,6 +25,8 @@ namespace adminBlazor.Services // Save the data await _http.PostAsJsonAsync("https://localhost:7234/api/User/", item); + + _logger.LogInformation("User API : call of method ADD. Creation of user ID : {Id}.", item.Id); } public async Task Count() @@ -31,12 +36,14 @@ namespace adminBlazor.Services public async Task> List(int currentPage, int pageSize) { + _logger.LogInformation("User API : call of method LIST."); return await _http.GetFromJsonAsync>($"https://localhost:7234/api/User/?currentPage={currentPage}&pageSize={pageSize}"); } public async Task GetById(int id) { - return await _http.GetFromJsonAsync($"https://localhost:7234/api/User/{id}"); + _logger.LogInformation("User API : call of method GetByID."); + return await _http.GetFromJsonAsync($"https://localhost:7234/api/User/{id}"); } public async Task Update(int id, UserModel model) @@ -44,11 +51,14 @@ namespace adminBlazor.Services // Get the item var item = UserFactory.Create(model); + _logger.LogInformation("User API : call of method UPDATE on User ID : {Id}.", id); + await _http.PutAsJsonAsync($"https://localhost:7234/api/User/{id}", item); } public async Task Delete(int id) { + _logger.LogInformation("User API : call of method DELETE on User ID : {Id}.", id); await _http.DeleteAsync($"https://localhost:7234/api/User/{id}"); } diff --git a/Project/adminBlazor/adminBlazor/appsettings.json b/Project/adminBlazor/adminBlazor/appsettings.json index d3eda73..0a3b356 100644 --- a/Project/adminBlazor/adminBlazor/appsettings.json +++ b/Project/adminBlazor/adminBlazor/appsettings.json @@ -8,9 +8,20 @@ ] }, "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "LogLevel": { + "Default": "Error", + "Microsoft": "Warning" + }, + "Debug": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting": "Trace" + } + }, + "EventSource": { + "LogLevel": { + "Default": "Warning" + } } }, "AllowedHosts": "*"