Début Logs

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

@ -12,7 +12,7 @@ namespace WF_WebAdmin.Pages
private UserLogin userLogin = new UserLogin();
[Inject]
public UserLogin uLogin { get; set; }
public UserLogin uLogin { get; set; }
private string ErrorConnexion;
@ -36,17 +36,17 @@ namespace WF_WebAdmin.Pages
{
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.Name = user.Name;
uLogin.Image = user.Image;
NavigationManager.NavigateTo(NavigationManager.BaseUri + "/accueil");
NavigationManager.NavigateTo(NavigationManager.BaseUri + "accueil");
return;
}
else
{
@ -59,9 +59,9 @@ namespace WF_WebAdmin.Pages
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 WF_WebAdmin.Data;
using WF_WebAdmin.Model;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
@ -16,6 +18,8 @@ builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddHttpClient();
builder.Services.AddScoped<UserLogin>();
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Services
.AddBlazorise()
.AddBootstrapProviders()

@ -43,6 +43,12 @@
</NavLink>
</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>
</div>

Loading…
Cancel
Save