static async Task Run(string server, string apiKey, string query, string from, string to) { var connection = new SeqConnection(server, apiKey); var now = DateTime.UtcNow; var rangeStartUtc = from != null ? DateTime.Parse(from) : now - TimeSpan.FromDays(1); DateTime? rangeEndUtc = to != null ? DateTime.Parse(to) : now; var result = await connection.Data.QueryCsvAsync(query, rangeStartUtc, rangeEndUtc); Console.WriteLine(result); }
static async Task Run(string server, string apiKey, string filter, int window, CancellationToken cancel) { var startedAt = DateTime.UtcNow; var connection = new SeqConnection(server, apiKey); string strict = null; if (filter != null) { var converted = await connection.Expressions.ToStrictAsync(filter); strict = converted.StrictExpression; } var result = await connection.Events.ListAsync(count: window, render: true, fromDateUtc: startedAt, filter: strict); // Since results may come late, we request an overlapping window and exclude // events that have already been printed. If the last seen ID wasn't returned // we assume the count was too small to cover the window. var lastPrintedBatch = new HashSet<string>(); string lastReturnedId = null; while (!cancel.IsCancellationRequested) { if (result.Count == 0) { await Task.Delay(TimeSpan.FromSeconds(1)); } else { var noOverlap = result.All(e => e.Id != lastReturnedId); if (noOverlap && lastReturnedId != null) Console.WriteLine("<window exceeded>"); foreach (var eventEntity in ((IEnumerable<EventEntity>)result).Reverse()) { if (lastPrintedBatch.Contains(eventEntity.Id)) { continue; } lastReturnedId = eventEntity.Id; var exception = ""; if (eventEntity.Exception != null) exception = Environment.NewLine + eventEntity.Exception; var ts = DateTimeOffset.Parse(eventEntity.Timestamp).ToLocalTime(); var color = ConsoleColor.White; switch (eventEntity.Level) { case "Verbose": case "Debug": color = ConsoleColor.Gray; break; case "Warning": color = ConsoleColor.Yellow; break; case "Error": case "Fatal": color = ConsoleColor.Red; break; } Console.ForegroundColor = color; Console.WriteLine("{0:G} [{1}] {2}{3}", ts, eventEntity.Level, eventEntity.RenderedMessage, exception); Console.ResetColor(); } lastPrintedBatch = new HashSet<string>(result.Select(e => e.Id)); } var fromDateUtc = lastReturnedId == null ? startedAt : DateTime.UtcNow.AddMinutes(-3); result = await connection.Events.ListAsync(count: window, render: true, fromDateUtc: fromDateUtc, filter: strict); } }