Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions Logging.Memory/src/Logging.Memory/LogForLevel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace Logging.Memory
{
Expand All @@ -10,9 +11,52 @@ internal LogForLevel(int maxLogsCount)
logList = new List<(DateTime time, string line)>(maxLogsCount);
}

internal int currentLogIndex = 0;
readonly ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();
int currentLogIndex = 0;
readonly List<(DateTime time, string line)> logList;

internal List<(DateTime time, string line)> logList;
/// <remarks>This method is thread-safe</remarks>
public void Append(DateTime time, string line, int maxLogCount)
{
this.rwLock.EnterWriteLock();
try
{
if (logList.Count < maxLogCount)
{
logList.Add((time, line));
}
else
{
logList[currentLogIndex] = (time, line);
}

if (currentLogIndex < maxLogCount - 1)
{
currentLogIndex++;
}
else
{
currentLogIndex = 0;
}
}
finally
{
this.rwLock.ExitWriteLock();
}
}

/// <remarks>This method is thread-safe</remarks>
public (DateTime time, string line)[] GetEntries()
{
this.rwLock.EnterReadLock();
try
{
return logList.ToArray();
}
finally
{
this.rwLock.ExitReadLock();
}
}
}
}
}
32 changes: 5 additions & 27 deletions Logging.Memory/src/Logging.Memory/MemoryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
/// </summary>
public class MemoryLogger : ILogger
{
private static readonly object lockObj = new object();

private Func<string, LogLevel, bool> filter;

private readonly Func<LogLevel, string, string, Exception, string> logLineFormatter = null;
Expand Down Expand Up @@ -59,7 +57,7 @@ public static List<string> LogList
get
{
return logsDictionary
.SelectMany(x => x.Value.logList)
.SelectMany(x => x.Value.GetEntries())
.OrderByDescending(x => x.time)
.Take(MaxLogCount)
.Reverse() // keep asc sort like in 1st version
Expand All @@ -76,7 +74,7 @@ public static List<string> LogList
{
if (logsDictionary.TryGetValue(logLevel, out var log))
{
return log.logList.OrderBy(x => x.time).ToList();
return log.GetEntries().OrderBy(x => x.time).ToList();
}
else
{
Expand All @@ -102,7 +100,7 @@ public static List<string> GetLog(LogLevel logLevel)
public static List<(DateTime time,string line)> GetLogGteWithTime(LogLevel minLogLevel)
{
return logsDictionary.Where(x => x.Key >= minLogLevel)
.SelectMany(x => x.Value.logList)
.SelectMany(x => x.Value.GetEntries())
.OrderBy(x => x.time).ToList();
}

Expand All @@ -124,7 +122,7 @@ public static List<string> GetLogGte(LogLevel minLogLevel)
public static List<(DateTime time, string line)> GetLogLteWithTime(LogLevel maxLogLevel)
{
return logsDictionary.Where(x => x.Key <= maxLogLevel)
.SelectMany(x => x.Value.logList)
.SelectMany(x => x.Value.GetEntries())
.OrderBy(x => x.time).ToList();
}

Expand Down Expand Up @@ -184,27 +182,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
if (!string.IsNullOrEmpty(message))
{
var preparedMessage = this.logLineFormatter(logLevel, Name, message, exception);
lock (lockObj)
{
if (currentLog.logList.Count < MaxLogCount)
{
currentLog.logList.Add((DateTime.Now, preparedMessage));
}
else
{
currentLog.logList[currentLog.currentLogIndex] = (DateTime.Now, preparedMessage);
}

if (currentLog.currentLogIndex < MaxLogCount - 1)
{
currentLog.currentLogIndex++;
}
else
{
currentLog.currentLogIndex = 0;
}
}

currentLog.Append(DateTime.Now, preparedMessage, MaxLogCount);
}
}
}
Expand Down