Finition des logs (fonction créé a rajouter sur tous les bouton pour la création des logs / logs enregister et afficher dans la page logs)

pull/31/head
Kentin BRONGNIART 3 months ago
parent 6a9afeb6f4
commit 25963b2c38

@ -1,70 +1,39 @@
using Microsoft.Extensions.Logging;
using System.Xml.Linq;
using static WF_WebAdmin.Model.LoggerSaveStub.CustomLoggerConfiguration;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic;
using System.Diagnostics;
using WF_WebAdmin.Pages;
using WF_WebAdmin.Service;
namespace WF_WebAdmin.Model
{
public sealed class LoggerSaveStub : ILogger
public static partial class LoggerSaveStub
{
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)
public static void Log(ILogger logs,LogLevel logLevel,string message)
{
if (!IsEnabled(logLevel))
{
return;
}
CustomLoggerConfiguration config = getCurrentConfig();
ILogsService logsService = new LogsServiceStub();
logsService.addLogs( new Logs( logLevel , message ) );
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;
}
}
logs.Log(logLevel, message );
}
}
}
}
/*
* [Inject]
* public ILogger< Class > Logger { get; set; }
*
* LoggerSaveStub.Log(Logger,LogLevel. level , message );
*
*
* LogLevel:
* Trace = 0,
* Debug = 1,
* Information = 2,
* Warning = 3,
* Error = 4,
* Critical = 5,
* None = 6,
*/

@ -0,0 +1,14 @@
namespace WF_WebAdmin.Model
{
public class Logs
{
public LogLevel LogLevel { get; set; }
public string Message { get; set; }
public Logs(LogLevel logLevel , string message) {
this.LogLevel=logLevel;
this.Message=message;
}
}
}

@ -10,8 +10,6 @@ namespace WF_WebAdmin.Pages
[Inject]
public ILogger<DeleteUser> Logger { get; set; }
public LoggerSaveStub logger { get; set; }
private List<User> users;
@ -36,8 +34,8 @@ namespace WF_WebAdmin.Pages
// ------- 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);
LoggerSaveStub.Log(Logger,LogLevel.Information, $"Demande de supretion de l utilisateur : { user.Name }");
userToDelete = user;
showPopupDelete = true;
}

@ -0,0 +1,34 @@
@page "/logs"
<h3>Logs</h3>
@if (logs is null)
{
<p>Aucun Logs</p>
}
@* else if (quotes.Count == 0)
{
<p>Aucune citation en attente de validation.</p>
} *@
else
{
<p>Citations en attente de validation :</p>
<table>
<thead>
<tr>
<th>LogLevel :</th>
<th>Message :</th>
</tr>
</thead>
<tbody>
@foreach (var log in logs)
{
<tr>
<td>@log.LogLevel</td>
<td>@log.Message</td>
</tr>
}
</tbody>
</table>
}

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Components;
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Pages
{
public partial class LogsPage
{
private Logs[] logs;
[Inject]
public HttpClient Http { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
protected override async Task OnInitializedAsync()
{
logs = await Http.GetFromJsonAsync<Logs[]>($"{NavigationManager.BaseUri}fake_data_logs.json");
}
}
}

@ -0,0 +1,17 @@
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Service
{
public interface ILogsService
{
public Task removeLogs(Logs logs);
public Task<List<Logs>> getAllLogs();
public Task<List<Logs>> getSomeLogs(int nb, int page);
public Task addLogs(Logs logs);
public Task<int> getNbLogs();
}
}

@ -0,0 +1,67 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text.Json;
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Service
{
public class LogsServiceStub : ILogsService
{
private readonly string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake_data_logs.json");
public async Task saveLogsJson(List<Logs> logs)
{
var json = JsonSerializer.Serialize(logs, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(_jsonFilePath, json);
}
public async Task addLogs(Logs logs)
{
var data = await getAllLogs();
data.Add(logs);
await saveLogsJson(data);
}
public async Task<List<Logs>> getAllLogs()
{
if (!File.Exists(_jsonFilePath))
{
Console.Out.WriteLine($"{_jsonFilePath} not found");
return new List<Logs>();
}
var json = await File.ReadAllTextAsync(_jsonFilePath);
return JsonSerializer.Deserialize<List<Logs>>(json) ?? new List<Logs>();
}
public async Task<int> getNbLogs()
{
throw new NotImplementedException();
}
public async Task<List<Logs>> getSomeLogs(int nb, int page)
{
var logs = await getAllLogs();
if ((page - 1) * nb + nb > logs.Count())
{
return logs.GetRange(logs.Count() - nb, nb);
}
return logs.GetRange((page - 1) * nb, nb);
}
public async Task removeLogs(Logs logs)
{
var data = await getAllLogs();
var l = data.FirstOrDefault(p => p.Message ==logs.Message && p.LogLevel==logs.LogLevel);
if (l != null)
{
data.Remove(l);
await saveLogsJson(data);
}
}
}
}

@ -0,0 +1,18 @@
[
{
"LogLevel": 1,
"Message": "Logs de test"
},
{
"LogLevel": 2,
"Message": "Demande de supretion de l utilisateur : admin"
},
{
"LogLevel": 2,
"Message": "Demande de supretion de l utilisateur : testeur"
},
{
"LogLevel": 2,
"Message": "Demande de supretion de l utilisateur : dev"
}
]
Loading…
Cancel
Save