Code · 86 lines · 2831 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86using LiteDB;
using MailSharp.MailClient.Models;
using Microsoft.Extensions.Logging;

namespace MailSharp.MailClient.Services.Logging;

public class LogQuery
{
	// Exact match, not a minimum threshold - the Maintenance log viewer's dropdown is meant to
	// isolate one level at a time (e.g. "just show me the errors"), not "this level and worse".
	public LogLevel? Level { get; set; }
	public string? Category { get; set; }
	public int? AccountId { get; set; }
	public DateTime? From { get; set; }
	public DateTime? To { get; set; }
	public string? Search { get; set; }
	public int Page { get; set; } = 1;
	public int PageSize { get; set; } = 50;
}

public class LogQueryResult
{
	public List<LogEntry> Items { get; set; } = [];
	public int TotalCount { get; set; }
}

public interface ILogStore
{
	void Add(LogEntry entry);
	LogQueryResult Query(LogQuery query);
	void PruneOlderThan(DateTime cutoffUtc);
	void Clear();
}

// Holds a single long-lived LiteDatabase connection for the app's lifetime (registered as a
// singleton), same pattern as the other *Store classes - a fresh connection per call would race on
// the OS file lock under concurrent requests.
public class LiteDbLogStore : ILogStore, IDisposable
{
	private readonly LiteDatabase _db;

	public LiteDbLogStore(IWebHostEnvironment env)
	{
		var dir = Path.Combine(env.ContentRootPath, "App_Data");
		Directory.CreateDirectory(dir);
		_db = new LiteDatabase(Path.Combine(dir, "logs.db"));

		Logs.EnsureIndex(x => x.Timestamp);
		Logs.EnsureIndex(x => x.Level);
		Logs.EnsureIndex(x => x.Category);
		Logs.EnsureIndex(x => x.AccountId);
	}

	private ILiteCollection<LogEntry> Logs => _db.GetCollection<LogEntry>("logs");

	public void Add(LogEntry entry) => Logs.Insert(entry);

	public LogQueryResult Query(LogQuery query)
	{
		var q = Logs.Query();

		if (query.Level != null) q = q.Where(x => x.Level == query.Level.Value);
		if (!string.IsNullOrWhiteSpace(query.Category)) q = q.Where(x => x.Category.Contains(query.Category));
		if (query.AccountId != null) q = q.Where(x => x.AccountId == query.AccountId.Value);
		if (query.From != null) q = q.Where(x => x.Timestamp >= query.From.Value);
		if (query.To != null) q = q.Where(x => x.Timestamp <= query.To.Value);
		if (!string.IsNullOrWhiteSpace(query.Search)) q = q.Where(x => x.Message.Contains(query.Search));

		var total = q.Count();
		var page = Math.Max(1, query.Page);
		var pageSize = Math.Clamp(query.PageSize, 1, 500);

		var items = q.OrderByDescending(x => x.Timestamp)
			.Offset((page - 1) * pageSize)
			.Limit(pageSize)
			.ToList();

		return new LogQueryResult { Items = items, TotalCount = total };
	}

	public void PruneOlderThan(DateTime cutoffUtc) => Logs.DeleteMany(x => x.Timestamp < cutoffUtc);

	public void Clear() => Logs.DeleteAll();

	public void Dispose() => _db.Dispose();
}