Add date filtering to client logs diagnostics
TaxBaik CI/CD / build-and-deploy (push) Has been cancelled
TaxBaik CI/CD / build-and-deploy (push) Has been cancelled
This commit is contained in:
@@ -5,6 +5,7 @@ namespace TaxBaik.Web.Endpoints.ClientLogs;
|
||||
public sealed class RecentClientLogEntry
|
||||
{
|
||||
public string? Timestamp { get; set; }
|
||||
public DateTimeOffset? OccurredAt { get; set; }
|
||||
public string? Level { get; set; }
|
||||
public string? Source { get; set; }
|
||||
public string? Message { get; set; }
|
||||
@@ -37,6 +38,8 @@ public sealed class GetRecentLogsEndpoint : EndpointWithoutRequest<IReadOnlyList
|
||||
var source = Query<string>("source");
|
||||
var route = Query<string>("route");
|
||||
var term = Query<string>("q");
|
||||
var start = ParseDate(Query<string>("start"));
|
||||
var end = ParseDate(Query<string>("end"));
|
||||
|
||||
var logDir = Path.Combine(_environment.ContentRootPath, "logs");
|
||||
if (!Directory.Exists(logDir))
|
||||
@@ -60,7 +63,7 @@ public sealed class GetRecentLogsEndpoint : EndpointWithoutRequest<IReadOnlyList
|
||||
continue;
|
||||
|
||||
var entry = Parse(line);
|
||||
if (Matches(entry, level, source, route, term))
|
||||
if (Matches(entry, level, source, route, term, start, end))
|
||||
entries.Add(entry);
|
||||
}
|
||||
}
|
||||
@@ -70,18 +73,21 @@ public sealed class GetRecentLogsEndpoint : EndpointWithoutRequest<IReadOnlyList
|
||||
|
||||
private static RecentClientLogEntry Parse(string line)
|
||||
{
|
||||
var entry = new RecentClientLogEntry();
|
||||
entry.Timestamp = ExtractTimestamp(line);
|
||||
entry.Level = ExtractBetween(line, "] [", "]");
|
||||
entry.Source = ExtractTokenAfter(line, "ClientLog ", 1);
|
||||
entry.Route = ExtractNamedValue(line, "Route=");
|
||||
entry.Screen = ExtractNamedValue(line, "Screen=");
|
||||
entry.Feature = ExtractNamedValue(line, "Feature=");
|
||||
entry.Action = ExtractNamedValue(line, "Action=");
|
||||
entry.Step = ExtractNamedValue(line, "Step=");
|
||||
entry.BuildVersion = ExtractNamedValue(line, "BuildVersion=");
|
||||
entry.Message = ExtractMessage(line);
|
||||
return entry;
|
||||
var timestamp = ExtractTimestamp(line);
|
||||
return new RecentClientLogEntry
|
||||
{
|
||||
Timestamp = timestamp,
|
||||
OccurredAt = ParseDate(timestamp),
|
||||
Level = ExtractBetween(line, "] [", "]"),
|
||||
Source = ExtractTokenAfter(line, "ClientLog ", 1),
|
||||
Route = ExtractNamedValue(line, "Route="),
|
||||
Screen = ExtractNamedValue(line, "Screen="),
|
||||
Feature = ExtractNamedValue(line, "Feature="),
|
||||
Action = ExtractNamedValue(line, "Action="),
|
||||
Step = ExtractNamedValue(line, "Step="),
|
||||
BuildVersion = ExtractNamedValue(line, "BuildVersion="),
|
||||
Message = ExtractMessage(line)
|
||||
};
|
||||
}
|
||||
|
||||
private static string? ExtractTimestamp(string line)
|
||||
@@ -120,7 +126,7 @@ public sealed class GetRecentLogsEndpoint : EndpointWithoutRequest<IReadOnlyList
|
||||
return clientIndex >= 0 ? line[(clientIndex + "ClientLog ".Length)..] : line;
|
||||
}
|
||||
|
||||
private static bool Matches(RecentClientLogEntry entry, string? level, string? source, string? route, string? term)
|
||||
private static bool Matches(RecentClientLogEntry entry, string? level, string? source, string? route, string? term, DateTimeOffset? start, DateTimeOffset? end)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(level) &&
|
||||
!string.Equals(entry.Level, level, StringComparison.OrdinalIgnoreCase))
|
||||
@@ -147,9 +153,29 @@ public sealed class GetRecentLogsEndpoint : EndpointWithoutRequest<IReadOnlyList
|
||||
return false;
|
||||
}
|
||||
|
||||
if (start is not null && (entry.OccurredAt is null || entry.OccurredAt < start))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (end is not null && (entry.OccurredAt is null || entry.OccurredAt > end))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static DateTimeOffset? ParseDate(string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return DateTimeOffset.TryParse(value, out var parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
private static string? ExtractNamedValue(string text, string prefix)
|
||||
{
|
||||
var index = text.IndexOf(prefix, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
Reference in New Issue
Block a user