Пример #1
0
        public async Task GetEventsPageAsync_no_props_returns_events_page()
        {
            // Arrange
            var db          = Guid.NewGuid().ToString();
            var settingsMoq = new Mock <ISejilSettings>();

            settingsMoq.SetupGet(p => p.SqliteDbPath).Returns(db);
            settingsMoq.SetupGet(p => p.PageSize).Returns(3);
            InitializeDatabse(db);
            var repository = new SejilRepository(new SejilSqlProvider(settingsMoq.Object), settingsMoq.Object);

            using (var conn = new SqliteConnection($"DataSource={db}"))
            {
                conn.Open();
                for (var i = 0; i < 10; i++)
                {
                    using var cmd   = conn.CreateCommand();
                    cmd.CommandText = $@"INSERT INTO log (id, message, messageTemplate, level, timestamp) 
                        VALUES ('{Guid.NewGuid().ToString()}', '{i}', '{i}', 'info', datetime(CURRENT_TIMESTAMP,'+{i} Hour'))";
                    cmd.ExecuteNonQuery();
                }
            }

            // Act
            var logs = await repository.GetEventsPageAsync(2, null, null);

            // Assert
            Assert.Equal(3, logs.Count());
            Assert.Equal("6", logs.ElementAt(0).Message);
            Assert.Empty(logs.ElementAt(0).Properties);
            Assert.Equal("5", logs.ElementAt(1).Message);
            Assert.Empty(logs.ElementAt(1).Properties);
            Assert.Equal("4", logs.ElementAt(2).Message);
            Assert.Empty(logs.ElementAt(2).Properties);
        }
Пример #2
0
        public async Task GetEventsPageAsync_returns_events_page()
        {
            // Arrange
            var db          = Guid.NewGuid().ToString();
            var settingsMoq = new Mock <ISejilSettings>();

            settingsMoq.SetupGet(p => p.SqliteDbPath).Returns(db);
            settingsMoq.SetupGet(p => p.PageSize).Returns(3);
            InitializeDatabse(db);
            var repository = new SejilRepository(new SejilSqlProvider(settingsMoq.Object), settingsMoq.Object);

            using (var conn = new SqliteConnection($"DataSource={db}"))
            {
                conn.Open();
                for (var i = 0; i < 10; i++)
                {
                    var id  = Guid.NewGuid().ToString();
                    var cmd = new SqliteCommand($@"INSERT INTO log (id, message, messageTemplate, level, timestamp) 
                        VALUES ('{id}', '{i}', '{i}', 'info', datetime(CURRENT_TIMESTAMP,'+{i} Hour'))", conn);
                    cmd.ExecuteNonQuery();

                    for (var j = 0; j < 2; j++)
                    {
                        cmd.CommandText = $@"INSERT INTO log_property (logId, name, value) VALUES 
                            ('{id}', 'n', '{i}_{j}')";
                        cmd.ExecuteNonQuery();
                    }
                }
            }

            // Act
            var logs = await repository.GetEventsPageAsync(4, null, null);

            // Assert
            Assert.Single(logs);
            Assert.Equal("0", logs.ElementAt(0).Message);
            Assert.NotNull(logs.ElementAt(0).Properties);
            Assert.Equal(2, logs.ElementAt(0).Properties.Count);
            Assert.Equal("0_0", logs.ElementAt(0).Properties.ElementAt(0).Value);
            Assert.Equal("0_1", logs.ElementAt(0).Properties.ElementAt(1).Value);
        }
Пример #3
0
        public async Task EmitBatchAsync_ignores_events_with_sejil_url_in_RequestPath_or_Path_properties(string propertyName)
        {
            // Arrange
            var db          = Guid.NewGuid().ToString();
            var settingsMoq = new Mock <ISejilSettings>();

            settingsMoq.SetupGet(p => p.SqliteDbPath).Returns(db);
            settingsMoq.SetupGet(p => p.PageSize).Returns(100);
            settingsMoq.SetupGet(p => p.Url).Returns("/sejil");
            var repository = new SejilRepository(new SejilSqlProvider(settingsMoq.Object), settingsMoq.Object);
            var sink       = new SejilSinkMock(settingsMoq.Object);

            var tokens = new List <MessageTemplateToken>
            {
                new PropertyToken(propertyName, "{" + propertyName + "}"),
            };

            var properties = new List <LogEventProperty>
            {
                new LogEventProperty(propertyName, new ScalarValue("/sejil/events")),
            };

            var messageTemplate = new MessageTemplate(tokens);

            var events = new List <LogEvent>
            {
                new LogEvent(DateTime.Now, LogEventLevel.Information, null, messageTemplate, properties),
            };

            // Act
            await sink.CallEmitBatchAsync(events);

            // Assert
            var logEvents = await repository.GetEventsPageAsync(1, null, null);

            Assert.Equal(0, logEvents.Count());
        }
Пример #4
0
        public async Task EmitBatchAsync_inserts_events_to_database()
        {
            // Arrange
            var db          = Guid.NewGuid().ToString();
            var settingsMoq = new Mock <ISejilSettings>();

            settingsMoq.SetupGet(p => p.SqliteDbPath).Returns(db);
            settingsMoq.SetupGet(p => p.PageSize).Returns(100);
            var repository = new SejilRepository(new SejilSqlProvider(settingsMoq.Object), settingsMoq.Object);
            var sink       = new SejilSinkMock(settingsMoq.Object);

            // Hello, {name}. Your # is {number}
            var tokens = new List <MessageTemplateToken>
            {
                new TextToken("Hello, ", 0),
                new PropertyToken("name", "{name}"),
                new TextToken(". Your # is ", 13),
                new PropertyToken("number", "{number}"),
            };

            var properties = new List <LogEventProperty>
            {
                new LogEventProperty("name", new ScalarValue("world")),
                new LogEventProperty("number", new ScalarValue(null))
            };

            var messageTemplate = new MessageTemplate(tokens);

            var timestamp1 = new DateTime(2017, 8, 3, 11, 44, 15, 542, DateTimeKind.Local);
            var timestamp2 = new DateTime(2017, 9, 3, 11, 44, 15, 542, DateTimeKind.Local);

            var events = new List <LogEvent>
            {
                new LogEvent(timestamp1, LogEventLevel.Information, null, messageTemplate, properties),
                new LogEvent(timestamp2, LogEventLevel.Debug, new Exception("error"), messageTemplate, properties),
            };

            // Act
            await sink.CallEmitBatchAsync(events);

            // Assert
            var logEvents = await repository.GetEventsPageAsync(1, null, null);

            Assert.Equal(2, logEvents.Count());

            var logEvent1 = logEvents.FirstOrDefault(p => p.Level == "Information");

            Assert.Equal("Hello, \"world\". Your # is null", logEvent1.Message);
            Assert.Equal("Hello, {name}. Your # is {number}", logEvent1.MessageTemplate);
            Assert.Equal("Information", logEvent1.Level);
            Assert.Equal(timestamp1, logEvent1.Timestamp);
            Assert.Null(logEvent1.Exception);
            Assert.Equal(2, logEvent1.Properties.Count());
            Assert.Equal(logEvent1.Id, logEvent1.Properties.ElementAt(0).LogId);
            Assert.Equal("name", logEvent1.Properties.ElementAt(0).Name);
            Assert.Equal("world", logEvent1.Properties.ElementAt(0).Value);
            Assert.Equal(logEvent1.Id, logEvent1.Properties.ElementAt(1).LogId);
            Assert.Equal("number", logEvent1.Properties.ElementAt(1).Name);
            Assert.Equal("null", logEvent1.Properties.ElementAt(1).Value);

            var logEvent2 = logEvents.FirstOrDefault(p => p.Level == "Debug");

            Assert.Equal("Hello, \"world\". Your # is null", logEvent2.Message);
            Assert.Equal("Hello, {name}. Your # is {number}", logEvent2.MessageTemplate);
            Assert.Equal("Debug", logEvent2.Level);
            Assert.Equal(timestamp2, logEvent2.Timestamp);
            Assert.Equal("System.Exception: error", logEvent2.Exception);
            Assert.Equal(2, logEvent2.Properties.Count());
            Assert.Equal(logEvent2.Id, logEvent2.Properties.ElementAt(0).LogId);
            Assert.Equal("name", logEvent2.Properties.ElementAt(0).Name);
            Assert.Equal("world", logEvent2.Properties.ElementAt(0).Value);
            Assert.Equal(logEvent2.Id, logEvent2.Properties.ElementAt(1).LogId);
            Assert.Equal("number", logEvent2.Properties.ElementAt(1).Name);
            Assert.Equal("null", logEvent2.Properties.ElementAt(1).Value);
        }