logging: cleanse messages in log files (#14433)

Co-authored-by: ilike2burnthing <59480337+ilike2burnthing@users.noreply.github.com>
This commit is contained in:
Bogdan
2023-06-08 18:09:16 +03:00
committed by GitHub
parent f42b76fb36
commit 23cff411ed
5 changed files with 118 additions and 20 deletions

View File

@@ -2,43 +2,43 @@ using System.Collections.Generic;
using System.Linq;
using Jackett.Common.Models;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils.Logging;
using NLog;
using NLog.Targets;
namespace Jackett.Common.Services
{
[Target("LogService")]
public class LogCacheService : TargetWithLayout, ILogCacheService
{
private static List<CachedLog> logs = new List<CachedLog>();
public void AddLog(LogEventInfo l)
{
lock (logs)
{
logs.Insert(0, new CachedLog()
{
Level = l.Level.Name,
Message = l.FormattedMessage,
When = l.TimeStamp
});
logs = logs.Take(200).ToList();
}
}
private static List<CachedLog> _Logs = new List<CachedLog>();
public List<CachedLog> Logs
{
get
{
lock (logs)
lock (_Logs)
{
return logs.ToList();
return _Logs.ToList();
}
}
}
protected override void Write(LogEventInfo logEvent) => AddLog(logEvent);
private static void AddLog(LogEventInfo logEvent)
{
lock (_Logs)
{
_Logs.Insert(0, new CachedLog
{
Level = logEvent.Level.Name,
Message = CleanseLogMessage.Cleanse(logEvent.FormattedMessage),
When = logEvent.TimeStamp
});
_Logs = _Logs.Take(200).ToList();
}
}
}
}