Ajout des logs
continuous-integration/drone/push Build is passing Details

blazor
Antoine JOURDAIN 1 year ago
parent 7652373f09
commit 220be1c9cd

@ -13,6 +13,7 @@ using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using adminBlazor.Models; using adminBlazor.Models;
using adminBlazor; using adminBlazor;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
@ -23,14 +24,12 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(); builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>(); builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddScoped<IDataService, DataApiService>(); builder.Services.AddScoped<IDataService, DataApiService>();
builder.Services.AddScoped<IDataService, DataLocalService>(); builder.Services.AddScoped<IDataService, DataLocalService>();
builder.Services.AddScoped<IVocListService, VocListLocalService>(); builder.Services.AddScoped<IVocListService, VocListLocalService>();
builder.Services.AddOptions(); builder.Services.AddOptions();
@ -41,67 +40,67 @@ builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddBlazoredLocalStorage(); builder.Services.AddBlazoredLocalStorage();
builder.Services.AddBlazoredModal(); builder.Services.AddBlazoredModal();
// Add the controller of the app // Add the controller of the app
builder.Services.AddControllers(); builder.Services.AddControllers();
// Add the localization to the app and specify the resources path // Add the localization to the app and specify the resources path
builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
// Configure the localtization // Configure the localization
builder.Services.Configure<RequestLocalizationOptions>(options => builder.Services.Configure<RequestLocalizationOptions>(options =>
{ {
// Set the default culture of the web site // Set the default culture of the web site
options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US")); options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"));
// Declare the supported culture // Declare the supported culture
options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("fr-FR") };
options.SupportedUICultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; options.SupportedUICultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("fr-FR") };
}); });
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Services builder.Services
.AddBlazorise() .AddBlazorise()
.AddBootstrapProviders() .AddBootstrapProviders()
.AddFontAwesomeIcons(); .AddFontAwesomeIcons();
var app = builder.Build();
var app = builder.Build(); // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
// Configure the HTTP request pipeline. {
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error"); 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. // 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.UseHsts();
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
// Get the current localization options // Get the current localization options
var options = ((IApplicationBuilder)app).ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); var options = ((IApplicationBuilder)app).ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
if (options?.Value != null) if (options?.Value != null)
{ {
// use the default localization // use the default localization
app.UseRequestLocalization(options.Value); app.UseRequestLocalization(options.Value);
} }
// Add the controller to the endpoint // Add the controller to the endpoint
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapControllers(); endpoints.MapControllers();
}); });
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run(); app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();

@ -9,6 +9,12 @@
public class AuthService : IAuthService public class AuthService : IAuthService
{ {
private static readonly List<AppUser> CurrentUser; private static readonly List<AppUser> CurrentUser;
private readonly ILogger<AuthService> _logger;
public AuthService(ILogger<AuthService> logger)
{
_logger = logger;
}
static AuthService() static AuthService()
{ {
@ -25,6 +31,7 @@
if (user == null) if (user == null)
{ {
_logger.LogWarning("Authentication failed, field invalid.");
throw new Exception("User name or password invalid !"); throw new Exception("User name or password invalid !");
} }
@ -45,13 +52,17 @@
if (user == null) if (user == null)
{ {
_logger.LogWarning("Authentication : login failed, field invalid.");
throw new Exception("User name or password invalid !"); throw new Exception("User name or password invalid !");
} }
_logger.LogInformation("Authentication : login success.");
} }
public void Register(RegisterRequest registerRequest) public void Register(RegisterRequest registerRequest)
{ {
CurrentUser.Add(new AppUser { UserName = registerRequest.UserName, Password = registerRequest.Password, Roles = new List<string> { "guest" } }); CurrentUser.Add(new AppUser { UserName = registerRequest.UserName, Password = registerRequest.Password, Roles = new List<string> { "guest" } });
_logger.LogInformation("Authentication : register success.");
} }
} }
} }

@ -8,11 +8,14 @@ namespace adminBlazor.Services
public class DataApiService : IDataService public class DataApiService : IDataService
{ {
private readonly HttpClient _http; private readonly HttpClient _http;
private readonly ILogger<DataApiService> _logger;
public DataApiService( public DataApiService(
HttpClient http) HttpClient http,
ILogger<DataApiService> logger)
{ {
_http = http; _http = http;
_logger = logger;
} }
public async Task Add(UserModel model) public async Task Add(UserModel model)
@ -22,6 +25,8 @@ namespace adminBlazor.Services
// Save the data // Save the data
await _http.PostAsJsonAsync("https://localhost:7234/api/User/", item); 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<int> Count() public async Task<int> Count()
@ -31,11 +36,13 @@ namespace adminBlazor.Services
public async Task<List<User>> List(int currentPage, int pageSize) public async Task<List<User>> List(int currentPage, int pageSize)
{ {
_logger.LogInformation("User API : call of method LIST.");
return await _http.GetFromJsonAsync<List<User>>($"https://localhost:7234/api/User/?currentPage={currentPage}&pageSize={pageSize}"); return await _http.GetFromJsonAsync<List<User>>($"https://localhost:7234/api/User/?currentPage={currentPage}&pageSize={pageSize}");
} }
public async Task<User> GetById(int id) public async Task<User> GetById(int id)
{ {
_logger.LogInformation("User API : call of method GetByID.");
return await _http.GetFromJsonAsync<User>($"https://localhost:7234/api/User/{id}"); return await _http.GetFromJsonAsync<User>($"https://localhost:7234/api/User/{id}");
} }
@ -44,11 +51,14 @@ namespace adminBlazor.Services
// Get the item // Get the item
var item = UserFactory.Create(model); 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); await _http.PutAsJsonAsync($"https://localhost:7234/api/User/{id}", item);
} }
public async Task Delete(int id) 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}"); await _http.DeleteAsync($"https://localhost:7234/api/User/{id}");
} }

@ -8,9 +8,20 @@
] ]
}, },
"Logging": { "Logging": {
"LogLevel": {
"Default": "Error",
"Microsoft": "Warning"
},
"Debug": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.Hosting": "Trace"
}
},
"EventSource": {
"LogLevel": {
"Default": "Warning"
}
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*"

Loading…
Cancel
Save