Début Logs

pull/31/head
Kentin BRONGNIART 5 months ago
parent a15a229374
commit 6a9afeb6f4

@ -0,0 +1,70 @@
using Microsoft.Extensions.Logging;
using System.Xml.Linq;
using static WF_WebAdmin.Model.LoggerSaveStub.CustomLoggerConfiguration;
namespace WF_WebAdmin.Model
{
public sealed class LoggerSaveStub : ILogger
{
internal class CustomLoggerConfiguration
{
public int EventId { get; set; }
public Dictionary<LogLevel, LogFormat> LogLevels { get; set; } =
new()
{
[LogLevel.Information] = LogFormat.Short,
[LogLevel.Warning] = LogFormat.Short,
[LogLevel.Error] = LogFormat.Long
};
public enum LogFormat
{
Short,
Long
}
}
private readonly string name;
private readonly Func<CustomLoggerConfiguration> getCurrentConfig;
public IDisposable BeginScope<TState>(TState state) => default!;
public bool IsEnabled(LogLevel logLevel) =>
getCurrentConfig().LogLevels.ContainsKey(logLevel);
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
CustomLoggerConfiguration config = getCurrentConfig();
if (config.EventId == 0 || config.EventId == eventId.Id)
{
switch (config.LogLevels[logLevel])
{
case LogFormat.Short:
Console.WriteLine($"{name}: {formatter(state, exception)}");
break;
case LogFormat.Long:
Console.WriteLine($"[{eventId.Id,2}: {logLevel,-12}] {name} - {formatter(state, exception)}");
break;
default:
// No-op
break;
}
}
}
}
}

@ -1,80 +1,88 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration.UserSecrets; using Microsoft.Extensions.Configuration.UserSecrets;
using WF_WebAdmin.Model; using Microsoft.Extensions.Logging;
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Pages
{ namespace WF_WebAdmin.Pages
public partial class DeleteUser {
{ public partial class DeleteUser
{
[Inject]
public ILogger<DeleteUser> Logger { get; set; }
public LoggerSaveStub logger { get; set; }
private List<User> users; private List<User> users;
private bool showPopupDelete = false; private bool showPopupDelete = false;
private User userToDelete = null; private User userToDelete = null;
private bool showPopupAdmin = false; private bool showPopupAdmin = false;
private User userToAdmin = null; private User userToAdmin = null;
[Inject] [Inject]
public HttpClient Http { get; set; } public HttpClient Http { get; set; }
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
users = await Http.GetFromJsonAsync<List<User>>($"{NavigationManager.BaseUri}fake-dataUsers.json"); users = await Http.GetFromJsonAsync<List<User>>($"{NavigationManager.BaseUri}fake-dataUsers.json");
} }
// ------- Popup remove user ------- // ------- Popup remove user -------
private void ShowConfirmation(User user) private void ShowConfirmation(User user)
{ {
userToDelete = user; Logger.LogInformation( "Demande de suprétion de l'utilisateur : { Name }",user.Name);
showPopupDelete = true; //logger.LogInformation( "Demande de suprétion de l'utilisateur : { Name }",user.Name);
} userToDelete = user;
showPopupDelete = true;
}
private async Task RemoveUser()
{
if (userToDelete != null) private async Task RemoveUser()
{ {
users.RemoveAll(u => u.Name == userToDelete.Name); if (userToDelete != null)
ClosePopup();
}
}
private void ClosePopup()
{
showPopupDelete = false;
showPopupAdmin = false;
}
// ------- Popup admin -------
private void ShowConfirmationAdmin(User user)
{
userToAdmin = user;
showPopupAdmin = true;
}
private async Task Admin()
{
if (!userToAdmin.IsAdmin)
{ {
userToAdmin.IsAdmin = true; users.RemoveAll(u => u.Name == userToDelete.Name);
ClosePopup(); ClosePopup();
} }
}
private void ClosePopup()
{
showPopupDelete = false;
showPopupAdmin = false;
}
// ------- Popup admin -------
private void ShowConfirmationAdmin(User user)
{
userToAdmin = user;
showPopupAdmin = true;
}
private async Task Admin()
{
if (!userToAdmin.IsAdmin)
{
userToAdmin.IsAdmin = true;
ClosePopup();
}
else else
{ {
userToAdmin.IsAdmin = false; userToAdmin.IsAdmin = false;
ClosePopup(); ClosePopup();
} }
} }
} }
} }

@ -12,7 +12,7 @@ namespace WF_WebAdmin.Pages
private UserLogin userLogin = new UserLogin(); private UserLogin userLogin = new UserLogin();
[Inject] [Inject]
public UserLogin uLogin { get; set; } public UserLogin uLogin { get; set; }
private string ErrorConnexion; private string ErrorConnexion;
@ -36,17 +36,17 @@ namespace WF_WebAdmin.Pages
{ {
foreach (var user in usersConnexion) foreach (var user in usersConnexion)
{ {
if(userLogin.Name == user.Name && userLogin.Mdp == user.Mdp) if (userLogin.Name == user.Name && userLogin.Mdp == user.Mdp)
{ {
if(user.IsAdmin) if (user.IsAdmin)
{ {
uLogin.Id = userLogin.Id; uLogin.Id = userLogin.Id;
uLogin.Name = user.Name; uLogin.Name = user.Name;
uLogin.Image = user.Image; uLogin.Image = user.Image;
NavigationManager.NavigateTo(NavigationManager.BaseUri + "/accueil"); NavigationManager.NavigateTo(NavigationManager.BaseUri + "accueil");
return; return;
} }
else else
{ {
@ -59,9 +59,9 @@ namespace WF_WebAdmin.Pages
ErrorConnexion = "Connexion échouée, le nom ou le mot de passe sont incorrectes"; ErrorConnexion = "Connexion échouée, le nom ou le mot de passe sont incorrectes";
} }
} }
} }
} }
} }
} }

@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
using WF_WebAdmin.Data; using WF_WebAdmin.Data;
using WF_WebAdmin.Model; using WF_WebAdmin.Model;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -16,6 +18,8 @@ builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddScoped<UserLogin>(); builder.Services.AddScoped<UserLogin>();
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Services builder.Services
.AddBlazorise() .AddBlazorise()
.AddBootstrapProviders() .AddBootstrapProviders()

@ -43,6 +43,12 @@
</NavLink> </NavLink>
</div> </div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="logs">
<span class="oi oi-list-rich" aria-hidden="true"></span> Logs
</NavLink>
</div>
</nav> </nav>
</div> </div>

Loading…
Cancel
Save