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,11 +1,17 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration.UserSecrets; using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.Logging;
using WF_WebAdmin.Model; 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;
@ -30,6 +36,8 @@ namespace WF_WebAdmin.Pages
// ------- Popup remove user ------- // ------- Popup remove user -------
private void ShowConfirmation(User 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; userToDelete = user;
showPopupDelete = true; showPopupDelete = true;
} }

@ -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,15 +36,15 @@ 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;
} }

@ -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