public Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("signal list", "-i signal-none"); Assert.Equal(1, exit); exit = runner.Exec("signal list", "-t Warnings"); Assert.Equal(0, exit); var output = runner.LastRunProcess.Output; Assert.Equal("signal-m33302 Warnings", output.Trim()); exit = runner.Exec("signal remove", "-t Warnings"); Assert.Equal(0, exit); exit = runner.Exec("signal list", "-i signal-m33302"); Assert.Equal(1, exit); exit = runner.Exec("signal list", "-t Warnings"); Assert.Equal(0, exit); return(Task.CompletedTask); }
public Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("user list"); Assert.Equal(0, exit); exit = runner.Exec("user list", "-n admin"); Assert.Equal(0, exit); var output = runner.LastRunProcess.Output; Assert.Equal("user-admin admin", output.Trim()); #if MULTIUSER_TESTS exit = runner.Exec("user create", "-n alice -d \"Alice Example\" -r \"User (read/write)\" -p test"); Assert.Equal(0, exit); var lines = new StringReader(runner.LastRunProcess.Output); lines.ReadLine(); // Skip password STDIN warning var id = lines.ReadLine()?.Split()[0]; Assert.NotNull(id); exit = runner.Exec("user remove", $"-i {id}"); Assert.Equal(0, exit); exit = runner.Exec("user list", "-n alice"); Assert.Equal(1, exit); #endif return(Task.CompletedTask); }
public async Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec( "user create", "-n alice -d \"Alice Example\" -r \"User (read/write)\" -p test@1234"); Assert.Equal(0, exit); var id = runner.LastRunProcess.Output.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); Assert.NotNull(id); var user = await connection.Users.FindAsync(id); Assert.All(user.RoleIds, r => r.StartsWith("role-", StringComparison.OrdinalIgnoreCase)); exit = runner.Exec("user remove", $"-i {id}"); Assert.Equal(0, exit); exit = runner.Exec("user list", "-i {id}"); Assert.Equal(1, exit); }
static Initializer() { var switchLevel = new LoggingLevelSwitch(); var config = new ConfigurationBuilder() .AddEnvironmentVariables() .Build(); var seqUri = config["Seq:Uri"]; var seqKey = config["Seq:ApiKey"]; Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .Enrich.WithProperty("Application", ApplicationName) .Enrich.WithProperty("Environment", "Unit Testing") .WriteTo.Seq( seqUri, apiKey: seqKey, controlLevelSwitch: switchLevel) .CreateLogger(); Seq = new SeqConnection( seqUri, apiKey: seqKey, true); }
public async Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("feed list", "-n nuget.org"); Assert.Equal(0, exit); exit = runner.Exec("feed list", "-i feed-missing"); Assert.Equal(1, exit); AssertExistence(runner, "Example", false); exit = runner.Exec("feed create", "-n Example -l \"https://example.com\" -u Test -p Pass"); Assert.Equal(0, exit); AssertExistence(runner, "Example", true); var feed = (await connection.Feeds.ListAsync()).Single(f => f.Name == "Example"); Assert.Equal("Example", feed.Name); Assert.Equal("https://example.com", feed.Location); Assert.Equal("Test", feed.Username); Assert.Null(feed.NewPassword); exit = runner.Exec("feed remove", "-n Example"); Assert.Equal(0, exit); AssertExistence(runner, "Example", false); }
/// <summary> /// Получить количество записей в seq по заданным условиям /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <CountResponse> GetCountOfLogs(LogsRequest request) { CountResponse response = new CountResponse(); _seqConnection = new SeqConnection(_configuration["SeqUrl"], "autxb7drQIW5afVHz4Nz"); if (!_seqConnection.EnsureConnectedAsync(new TimeSpan(100)).IsCompleted) { _seqConnection = new SeqConnection(_configuration["SeqUrl"], "autxb7drQIW5afVHz4Nz"); } List <LogsRequest> logs = new List <LogsRequest>(); //var installedApps = _seqConnection.Apps.ListAsync(); //string login = request.Login; //string reqPath = request.RequestPath; //var resultSet = await _seqConnection.Events.ListAsync( // filter: $"login = '******' AND RequestPath = '{reqPath}'", // render: true, // fromDateUtc: request.BeginDate, // toDateUtc: request.EndDate, // count: 100) ; //все записи в базе по указанным фильтрам string login = request.Login ?? "noLogin"; string reqPath = request.RequestPath ?? ""; string lastReadEventId = null; while (true) { var resultSetFull = await _seqConnection.Events.InSignalAsync( filter : $"login = '******' AND RequestPath = '{reqPath}'", render : true, fromDateUtc : request.BeginDate, toDateUtc : request.EndDate, afterId : lastReadEventId, count : 100000); //foreach (var evt in resultSetFull.Events) // logs.Add(new LogRequest() { TimeStamp = Convert.ToDateTime(evt.Timestamp), Message = evt.RenderedMessage }); response.Count += resultSetFull.Events.Count(); if (resultSetFull.Statistics.Status != ResultSetStatus.Partial) { break; } lastReadEventId = resultSetFull.Statistics.LastReadEventId; } //foreach (var evt in resultSet) //{ // //можно сделать фильтр по уровню ошибки // //if (evt.Level == "Error") // logs.Add(new LogRequest() { TimeStamp = Convert.ToDateTime(evt.Timestamp), Message = evt.RenderedMessage }); //} return(response); }
public static async Task <string?> ImportAsync( IEnumerable <EntityTemplate> templates, SeqConnection connection, IReadOnlyDictionary <string, JsonTemplate> args, TemplateImportState state, bool merge) { var ordering = new[] { "users", "signals", "apps", "appinstances", "dashboards", "sqlqueries", "workspaces", "retentionpolicies" }.ToList(); var sorted = templates.OrderBy(t => ordering.IndexOf(t.ResourceGroup)); var apiRoot = await connection.Client.GetRootAsync(); var functions = new EntityTemplateFunctions(state, args); foreach (var entityTemplateFile in sorted) { var err = await ApplyTemplateAsync(entityTemplateFile, functions, state, connection, apiRoot, merge); if (err != null) { return(err); } } return(null); }
async Task <SeqConnection> TryConnectAsync() { SeqConnection connection; if (_connectUsername != null) { if (_connection.IsApiKeySpecified) { Log.Error("The `connect-username` and `apikey` options are mutually exclusive"); return(null); } if (_connectPasswordStdin) { if (_connectPassword != null) { Log.Error("The `connect-password` and `connect-password-stdin` options are mutually exclusive"); return(null); } _connectPassword = await Console.In.ReadLineAsync(); } var(url, _) = _connectionFactory.GetConnectionDetails(_connection); connection = new SeqConnection(url); await connection.Users.LoginAsync(_connectUsername, _connectPassword ?? ""); } else { connection = _connectionFactory.Connect(_connection); } return(connection); }
static void SetupSeq() { PrintTitle("Setting up Seq"); var connection = new SeqConnection(SeqUrl); var entity = new ApiKeyEntity { Title = AppName, AssignedPermissions = new HashSet <Permission> { Permission.Ingest }, InputSettings = new InputSettingsPart { MinimumLevel = LogEventLevel.Debug, AppliedProperties = new List <InputAppliedPropertyPart> { new InputAppliedPropertyPart { Name = "app", Value = AppName } } } }; Task("Add API key", () => { var result = connection.ApiKeys.AddAsync(entity).Result; GeneratedOutput.Add("SeqConfiguration::ApiKey", result.Token); }); }
static async Task <bool> ConnectAsync(SeqConnection connection, bool throwOnFailure) { HttpStatusCode statusCode; try { statusCode = (await connection.Client.HttpClient.GetAsync("/api")).StatusCode; } catch { if (throwOnFailure) { throw; } return(false); } if (statusCode == HttpStatusCode.OK) { return(true); } if (!throwOnFailure) { return(false); } throw new Exception($"Could not connect to the Seq API endpoint: {statusCode}."); }
public async Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("retention list"); Assert.Equal(0, exit); exit = runner.Exec("retention list", "-i retentionpolicy-missing"); Assert.Equal(1, exit); exit = runner.Exec("retention create", "--after 10h --delete-all-events"); Assert.Equal(0, exit); var id = runner.LastRunProcess.Output.Trim(); exit = runner.Exec("retention list", $"-i {id}"); Assert.Equal(0, exit); var policy = await connection.RetentionPolicies.FindAsync(id); Assert.Null(policy.RemovedSignalExpression); Assert.Equal(TimeSpan.FromHours(10), policy.RetentionTime); exit = runner.Exec("retention remove", $"-i {id}"); Assert.Equal(0, exit); exit = runner.Exec("retention list", $"-i {id}"); Assert.Equal(1, exit); }
public async Task <List <UserResponse> > GetCountForFieldsAndRequestPath(FromToRequest request) { //запрашиваем список логинов select distinct(login) from stream //делаем запрос на каждый логин сколько раз он входил по фильтру //RequestPath = '/api/data/GetInitialData' and SourceContext = 'Authentication.Service.SerilogMiddleware' //сохраняем результат в словарь логин: количество входов _seqConnection = new SeqConnection(_configuration["SeqUrl"], "autxb7drQIW5afVHz4Nz"); await _seqConnection.Users.LoginAsync("admin", "vfr4ESZz"); if (!_seqConnection.EnsureConnectedAsync(new TimeSpan(100)).IsCompleted) { _seqConnection = new SeqConnection(_configuration["SeqUrl"], "autxb7drQIW5afVHz4Nz"); } string logins = await QueryToSeq("select distinct(login) from stream", request.From, request.To); List <string> loginsList = logins.Split("\n").ToList(); List <string> clearLoginsList = new List <string>(); Regex r = new Regex("(?:[^a-z0-9 _а-я]|(?<=['\"]))", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); for (int i = 0; i < loginsList.Count; i++) { clearLoginsList.Add(r.Replace(loginsList[i], string.Empty)); } List <UserResponse> login_count = new List <UserResponse>(); var now = DateTime.UtcNow; foreach (var login in clearLoginsList) { if (!string.IsNullOrWhiteSpace(login)) { if (!login.Contains("distinct")) { if (!login.Contains("noLogin")) { var res = await StrictQuery($"RequestPath = '/api/data/GetInitialData' and SourceContext = 'Authentication.Service.SerilogMiddleware' and login = '******'", request.From != null?request.From : now - TimeSpan.FromDays(1), request.To != null?request.To : now ); var stats = new Dictionary <string, object>(); stats.Add("LoginsCount", res.Count); login_count.Add(new UserResponse() { Login = login, Stats = stats }); } } } } return(login_count); }
public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("node health"); Assert.Equal(0, exit); Assert.StartsWith("HTTP 1.1 200 OK", runner.LastRunProcess !.Output); return(Task.CompletedTask); }
public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("node demote --verbose --wait --confirm"); Assert.Equal(1, exit); Assert.Contains("No cluster node is in the leader role", runner.LastRunProcess !.Output); return(Task.CompletedTask); }
public static async Task <int> ShipEvents( SeqConnection connection, ILogEventReader reader, List <ILogEventEnricher> enrichers, InvalidDataHandling invalidDataHandling, SendFailureHandling sendFailureHandling, Func <LogEvent, bool> filter = null) { if (connection == null) { throw new ArgumentNullException(nameof(connection)); } if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (enrichers == null) { throw new ArgumentNullException(nameof(enrichers)); } var batch = await ReadBatchAsync(reader, filter, BatchSize, invalidDataHandling); while (true) { var sendSucceeded = false; try { sendSucceeded = await SendBatchAsync( connection, batch.LogEvents, enrichers, sendFailureHandling != SendFailureHandling.Ignore); } catch (Exception ex) { if (sendFailureHandling != SendFailureHandling.Ignore) { Log.Error(ex, "Failed to send an event batch"); } } if (!sendSucceeded && sendFailureHandling == SendFailureHandling.Fail) { return(1); } if (batch.IsLast) { break; } batch = await ReadBatchAsync(reader, filter, BatchSize, invalidDataHandling); } return(0); }
public void WhenConstructedTheHandlerConfigurationCallbackIsCalled() { var callCount = 0; using var _ = new SeqConnection("https://test.example.com", null, handler => { Assert.NotNull(handler); ++callCount; }); Assert.Equal(1, callCount); }
public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var filename = _testDataFolder.ItemPath("license.txt"); File.WriteAllText(filename, "Ceci n'est pas une licence"); runner.Exec("license apply", $"--certificate=\"{filename}\""); Assert.Equal( "The command failed: 400 - The license format is invalid: data precedes any keys.", runner.LastRunProcess.Output.Trim()); return(Task.CompletedTask); }
public async Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("sample setup", "--confirm"); Assert.Equal(0, exit); var sampleWorkspace = (await connection.Workspaces.ListAsync(shared: true)) .SingleOrDefault(w => w.Title == "Sample"); Assert.NotNull(sampleWorkspace); }
public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("node list --json"); Assert.Equal(0, exit); Assert.Contains("\"Role\":", runner.LastRunProcess !.Output); Assert.Contains("\"Name\":", runner.LastRunProcess !.Output); Assert.Contains("\"StateDescription\":", runner.LastRunProcess !.Output); return(Task.CompletedTask); }
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); }
/// <summary> /// Вернуть записи логов в определенном формате /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <List <ErrorResponse> > GetLogsInFormat(DiasErrorsRequest request) { List <ErrorResponse> errorResponses = new List <ErrorResponse>(); DateTime temp; _seqConnection = new SeqConnection(_configuration["SeqUrl"], "autxb7drQIW5afVHz4Nz"); if (!_seqConnection.EnsureConnectedAsync(new TimeSpan(100)).IsCompleted) { _seqConnection = new SeqConnection(_configuration["SeqUrl"], "autxb7drQIW5afVHz4Nz"); } List <LogsRequest> logs = new List <LogsRequest>(); //все записи в базе по указанным фильтрам string lastReadEventId = null; string level = "Error"; while (true) { var resultSetFull = await _seqConnection.Events.InSignalAsync( filter : $"@Level = '{level}'", render : true, fromDateUtc : request.BeginDate, toDateUtc : request.EndDate, afterId : lastReadEventId, count : request.Count == 0? 100 : request.Count); foreach (var evt in resultSetFull.Events) { DateTime.TryParse(evt.Timestamp, out temp); errorResponses.Add(new ErrorResponse() { Time = temp, Header = evt.Properties.Find(x => x.Name == "header")?.Value?.ToString() ?? (evt.Properties.Find(x => x.Name == "SourceContext")?.Value?.ToString() ?? "System"), Host = evt.Properties.Find(x => x.Name == "host")?.Value?.ToString() ?? "" + " " + $"({evt.Properties.Find(x => x.Name == "ip")?.Value?.ToString() ?? ""})", Login = evt.Properties.Find(x => x.Name == "login")?.Value?.ToString() ?? "", Message = evt.Properties.Find(x => x.Name == "message")?.Value?.ToString() ?? evt.RenderedMessage }); } if (resultSetFull.Statistics.Status != ResultSetStatus.Partial) { break; } lastReadEventId = resultSetFull.Statistics.LastReadEventId; } return(errorResponses); }
static async Task Run(string src, string srcKey, string dst, string dstKey) { var srcConnection = new SeqConnection(src, srcKey); var dstConnection = new SeqConnection(dst, dstKey); var dstSignals = new Dictionary <string, SignalEntity>(); foreach (var dstSignal in await dstConnection.Signals.ListAsync()) { if (dstSignals.ContainsKey(dstSignal.Title)) { Console.WriteLine($"The destination server has more than one signal named '{dstSignal.Title}'; only one copy of the signal will be updated."); continue; } dstSignals.Add(dstSignal.Title, dstSignal); } var count = 0; foreach (var signal in await srcConnection.Signals.ListAsync()) { SignalEntity target; if (dstSignals.TryGetValue(signal.Title, out target)) { if (target.IsRestricted) { Console.WriteLine($"Skipping restricted signal '{signal.Title}' ({target.Id})"); continue; } Console.WriteLine($"Updating existing signal '{signal.Title}' ({target.Id})"); } else { Console.WriteLine($"Creating new signal '{signal.Title}'"); target = await dstConnection.Signals.TemplateAsync(); } target.Title = signal.Title; target.Filters = signal.Filters; target.TaggedProperties = signal.TaggedProperties; target.Description = signal.Description + " (copy)"; await(target.Id != null ? dstConnection.Signals.UpdateAsync(target) : dstConnection.Signals.AddAsync(target)); ++count; } Console.WriteLine($"Done, {count} signals updated."); }
public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("apikey create", "-t Test"); Assert.Equal(0, exit); exit = runner.Exec("apikey list", "-t Test --json --no-color"); Assert.Equal(0, exit); var output = runner.LastRunProcess.Output; Assert.Contains("\"AssignedPermissions\": [\"Ingest\"]", output); return(Task.CompletedTask); }
public async Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var inputFiles = Path.Combine("Data", "log-*.txt"); var exit = runner.Exec("ingest", $"-i {inputFiles}"); Assert.Equal(0, exit); var events = await connection.Events.ListAsync(); Assert.Equal(4, events.Count); }
public async Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("workspace list", "-i workspace-none"); Assert.Equal(1, exit); exit = runner.Exec("workspace list", "-t Example"); Assert.Equal(0, exit); var output = runner.LastRunProcess.Output; Assert.Equal("", output.Trim()); var items = ""; var dashboard = (await connection.Dashboards.ListAsync(shared: true)).First(); items += $" --dashboard={dashboard.Id}"; var query = (await connection.SqlQueries.ListAsync(shared: true)).First(); items += $" --query={query.Id}"; foreach (var signal in (await connection.Signals.ListAsync(shared: true)).Take(2)) { items += $" --signal={signal.Id}"; } exit = runner.Exec("workspace create", $"-t Example {items}"); Assert.Equal(0, exit); exit = runner.Exec("workspace list", "-t Example"); Assert.Equal(0, exit); output = runner.LastRunProcess.Output; Assert.Contains("Example", output.Trim()); var workspace = (await connection.Workspaces.ListAsync(shared: true)).Single(w => w.Title == "Example"); Assert.Single(workspace.Content.DashboardIds); Assert.Single(workspace.Content.QueryIds); Assert.Equal(2, workspace.Content.SignalIds.Count); exit = runner.Exec("workspace remove", "-t Example"); Assert.Equal(0, exit); }
/// <summary> /// Запрос none strict /// </summary> /// <param name="query"></param> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> private async Task <string> QueryToSeq(string query, DateTime from, DateTime to) { if (!_seqConnection.EnsureConnectedAsync(new TimeSpan(100)).IsCompleted) { _seqConnection = new SeqConnection(_configuration["SeqUrl"], "autxb7drQIW5afVHz4Nz"); } var now = DateTime.UtcNow; var rangeStartUtc = from != null ? from : now - TimeSpan.FromDays(1); DateTime?rangeEndUtc = to != null ? to : now; var result = await _seqConnection.Data.QueryCsvAsync("select distinct(login) from stream", rangeStartUtc, rangeEndUtc); Console.WriteLine(result); return(result); }
public static async Task EnsureConnected(this SeqConnection connection) { if (connection == null) { throw new ArgumentNullException(nameof(connection)); } var started = DateTime.UtcNow; var wait = TimeSpan.FromMilliseconds(100); var waitLimit = TimeSpan.FromSeconds(30); var deadline = started.Add(waitLimit); while (!await ConnectAsync(connection, DateTime.UtcNow > deadline)) { await Task.Delay(wait); } }
public async Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("signal create", "-t TestSignal -f \"@Exception is not null\" -c Column1 -c \"round(Property2, 1)\""); Assert.Equal(0, exit); var signals = await connection.Signals.ListAsync(shared : true); var testSignal = signals.First(x => x.Title == "TestSignal"); Assert.Equal("@Exception is not null", testSignal.Filters[0].Filter); Assert.Equal("Column1", testSignal.Columns[0].Expression); Assert.Equal("round(Property2, 1)", testSignal.Columns[1].Expression); }
public async Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var exit = runner.Exec("log", "-m \"Hello, {Name}!\" -p Name=world"); Assert.Equal(0, exit); var events = await connection.Events.ListAsync(render : true, filter : "Name = 'world'"); Assert.Single(events); var hello = events.Single(); Assert.Equal("Hello, world!", hello.RenderedMessage); }
public async Task ExecuteAsync( SeqConnection connection, ILogger logger, CliCommandRunner runner) { var inputFile = Path.Combine("Data", "events.ndjson"); Assert.True(File.Exists(inputFile)); var exit = runner.Exec("ingest", $"--json -i {inputFile}"); Assert.Equal(0, exit); var events = await connection.Events.ListAsync(); Assert.Equal(15, events.Count); }