Created
January 12, 2019 18:35
-
-
Save tompazourek/54299439bad6a5fe538d00fc241f95fd to your computer and use it in GitHub Desktop.
Program that downloads data from a Seq instance and stores it in a JSON file (CLEF format)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Seq.Api; | |
namespace SeqExportAllToJson | |
{ | |
internal class Program | |
{ | |
private static async Task Main() | |
{ | |
var connection = new SeqConnection("http://localhost:5341"); | |
var now = DateTimeOffset.Now; | |
var currentDate = now.UtcDateTime.Date; | |
var minDate = now.UtcDateTime.AddDays(-180).Date; | |
var events = await connection.LoadResourceGroupAsync("Events"); | |
var targetPath = Path.Combine(Environment.CurrentDirectory, "export"); | |
Directory.CreateDirectory(targetPath); | |
var incrementTimeSpan = TimeSpan.FromHours(1); | |
while (true) | |
{ | |
var fromDateUtcString = currentDate.ToString("O"); | |
var toDateUtcString = currentDate.Add(incrementTimeSpan).ToString("O"); | |
var clefString = await connection.Client.GetStringAsync(events, "InSignal", new Dictionary<string, object> | |
{ | |
["count"] = 1_000_000, | |
["clef"] = true, | |
["fromDateUtc"] = fromDateUtcString, | |
["toDateUtc"] = toDateUtcString, | |
}); | |
Console.WriteLine($"From '{fromDateUtcString}' to '{toDateUtcString}' read {clefString.Length} characters."); | |
if (!string.IsNullOrWhiteSpace(clefString)) | |
{ | |
var fileName = $"seq-clef-{currentDate:yyyyMMddHHmmss}.json"; | |
var filePath = Path.Combine(targetPath, fileName); | |
using (var file = new FileStream(filePath, FileMode.Create)) | |
using (var fileWriter = new StreamWriter(file)) | |
{ | |
await fileWriter.WriteAsync(clefString); | |
} | |
} | |
currentDate = currentDate.Subtract(incrementTimeSpan); | |
if (currentDate < minDate) | |
break; | |
} | |
Console.WriteLine("Finished."); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment