コード例 #1
0
        public async Task IncorrectDatabaseConnectionStringWithSchemaLogShouldThrowException()
        {
            const string SchemaName = "Logs4";
            const string TableName  = "LogsWithSchema4";

            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };
            var testObject2 = new TestObjectType2 {
                DateProp = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>();

            var invalidConnectionString = ConnectionString.Replace("Port=", "Port=1");
            var logger = new LoggerConfiguration().AuditTo.PostgreSQL(
                invalidConnectionString,
                TableName,
                columnProps,
                schemaName: SchemaName,
                needAutoCreateTable: true).Enrich.WithMachineName().CreateLogger();

            logger.Information(
                "Test{testNo}: {@testObject} test2: {@testObject2} testStr: {@testStr:l}",
                1,
                testObject,
                testObject2,
                "stringValue");
            Log.CloseAndFlush();
            await Task.Delay(1000);
        }
コード例 #2
0
        public async Task AutoCreateTableIsTrueShouldCreateTable()
        {
            const string SchemaName = "Logs1";
            const string TableName  = "LogsWithSchema1";

            this.databaseHelper.RemoveTable(SchemaName, TableName);

            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };
            var testObject2 = new TestObjectType2 {
                DateProp = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>
            {
                { "Message", new RenderedMessageColumnWriter() },
                { "MessageTemplate", new MessageTemplateColumnWriter() },
                { "Level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
                { "RaiseDate", new TimestampColumnWriter() },
                { "Exception", new ExceptionColumnWriter() },
                { "Properties", new LogEventSerializedColumnWriter() },
                { "PropertyTest", new PropertiesColumnWriter(NpgsqlDbType.Text) },
                {
                    "IntPropertyTest",
                    new SinglePropertyColumnWriter("testNo", PropertyWriteMethod.Raw, NpgsqlDbType.Integer)
                },
                { "MachineName", new SinglePropertyColumnWriter("MachineName", format: "l") }
            };

            var logger = new LoggerConfiguration().WriteTo.PostgreSQL(
                ConnectionString,
                TableName,
                columnProps,
                schemaName: SchemaName,
                needAutoCreateTable: true).Enrich.WithMachineName().CreateLogger();

            const long RowsCount = 50;

            for (var i = 0; i < RowsCount; i++)
            {
                logger.Information(
                    "Test{testNo}: {@testObject} test2: {@testObject2} testStr: {@testStr:l}",
                    i,
                    testObject,
                    testObject2,
                    "stringValue");
            }

            Log.CloseAndFlush();
            await Task.Delay(10000);

            var actualRowsCount = this.databaseHelper.GetTableRowsCount(SchemaName, TableName);

            Assert.AreEqual(RowsCount, actualRowsCount);
        }
コード例 #3
0
        public void AutoCreateTableIsTrueShouldCreateTableInsert()
        {
            this.dbHelper.RemoveTable(TableName);

            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };

            var testObj2 = new TestObjectType2 {
                DateProp = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>
            {
                { "Message", new RenderedMessageColumnWriter() },
                { "MessageTemplate", new MessageTemplateColumnWriter() },
                { "Level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
                { "RaiseDate", new TimestampColumnWriter() },
                { "Exception", new ExceptionColumnWriter() },
                { "Properties", new LogEventSerializedColumnWriter() },
                { "PropertyTest", new PropertiesColumnWriter(NpgsqlDbType.Text) },
                {
                    "IntPropertyTest",
                    new SinglePropertyColumnWriter(
                        "testNo",
                        PropertyWriteMethod.Raw,
                        NpgsqlDbType.Integer)
                },
                { "MachineName", new SinglePropertyColumnWriter("MachineName", format: "l") }
            };

            var logger = new LoggerConfiguration().WriteTo
                         .PostgreSql(ConnectionString, TableName, columnProps, needAutoCreateTable: true, useCopy: false).Enrich
                         .WithMachineName().CreateLogger();

            const int RowsCount = 1;

            for (var i = 0; i < RowsCount; i++)
            {
                logger.Information(
                    "Test{testNo}: {@testObject} test2: {@testObj2} testStr: {@testStr:l}",
                    i,
                    testObject,
                    testObj2,
                    "stringValue");
            }

            logger.Dispose();

            var actualRowsCount = this.dbHelper.GetTableRowsCount(TableName);

            Assert.Equal(RowsCount, actualRowsCount);
        }
コード例 #4
0
        public void ColumnsAndTableWithDifferentCaseName_ShouldCreateTableAndIsertEvents()
        {
            const string tableName  = "LogsAutoCreatedWithSchema";
            const string schemaName = "Logs";

            var fullTableName = $"\"{schemaName}\".\"{tableName}\"";

            _dbHelper.RemoveTable(fullTableName);

            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };

            var testObj2 = new TestObjectType2 {
                DateProp1 = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>
            {
                { "Message", new RenderedMessageColumnWriter() },
                { "MessageTemplate", new MessageTemplateColumnWriter() },
                { "Level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
                { "RaiseDate", new TimestampColumnWriter() },
                { "\"Exception\"", new ExceptionColumnWriter() },
                { "Properties", new LogEventSerializedColumnWriter() },
                { "PropsTest", new PropertiesColumnWriter(NpgsqlDbType.Text) },
                { "IntPropTest", new SinglePropertyColumnWriter("testNo", PropertyWriteMethod.Raw, NpgsqlDbType.Integer) },
                { "MachineName", new SinglePropertyColumnWriter("MachineName", format: "l") }
            };

            const int rowsCount = 50;

            using (var logger =
                       new LoggerConfiguration().WriteTo.PostgreSQL(
                           _connectionString,
                           tableName,
                           columnProps,
                           schemaName: schemaName,
                           needAutoCreateTable: true,
                           respectCase: true)
                       .Enrich.WithMachineName()
                       .CreateLogger())
            {
                for (var i = 0; i < rowsCount; i++)
                {
                    logger.Information("Test{testNo}: {@testObject} test2: {@testObj2} testStr: {@testStr:l}", i, testObject, testObj2, "stringValue");
                }
            }

            var actualRowsCount = _dbHelper.GetTableRowsCount(fullTableName);

            Assert.Equal(rowsCount, actualRowsCount);
        }
コード例 #5
0
        public void AutoCreateTableIsTrue_ShouldCreateTable()
        {
            const string tableName = "logs_auto_created_w_schema";

            var fullTableName = $"{_schemaName}.{tableName}";

            _dbHelper.RemoveTable(fullTableName);

            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };

            var testObj2 = new TestObjectType2 {
                DateProp1 = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>
            {
                { "message", new RenderedMessageColumnWriter() },
                { "message_template", new MessageTemplateColumnWriter() },
                { "level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
                { "raise_date", new TimestampColumnWriter() },
                { "exception", new ExceptionColumnWriter() },
                { "properties", new LogEventSerializedColumnWriter() },
                { "props_test", new PropertiesColumnWriter(NpgsqlDbType.Text) },
                { "int_prop_test", new SinglePropertyColumnWriter("testNo", PropertyWriteMethod.Raw, NpgsqlDbType.Integer) },
                { "machine_name", new SinglePropertyColumnWriter("MachineName", format: "l") }
            };

            var logger =
                new LoggerConfiguration().WriteTo.PostgreSQL(
                    _connectionString,
                    tableName,
                    columnProps,
                    schemaName: _schemaName,
                    needAutoCreateTable: true)
                .Enrich.WithMachineName()
                .CreateLogger();

            const int rowsCount = 50;

            for (var i = 0; i < rowsCount; i++)
            {
                logger.Information("Test{testNo}: {@testObject} test2: {@testObj2} testStr: {@testStr:l}", i, testObject, testObj2, "stringValue");
            }

            logger.Dispose();

            var actualRowsCount = _dbHelper.GetTableRowsCount(fullTableName);

            Assert.Equal(rowsCount, actualRowsCount);
        }
コード例 #6
0
        public void Write50Events_ShouldInsert50EventsToDb()
        {
            _dbHelper.ClearTable(_tableName);


            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };

            var testObj2 = new TestObjectType2 {
                DateProp1 = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>
            {
                { "message", new RenderedMessageColumnWriter() },
                { "message_template", new MessageTemplateColumnWriter() },
                { "level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
                { "raise_date", new TimestampColumnWriter() },
                { "exception", new ExceptionColumnWriter() },
                { "properties", new LogEventSerializedColumnWriter() },
                { "props_test", new PropertiesColumnWriter(NpgsqlDbType.Text) },
                { "machine_name", new SinglePropertyColumnWriter("MachineName", format: "l") }
            };

            var logger =
                new LoggerConfiguration().WriteTo.PostgreSQL(_connectionString, _tableName, columnProps)
                .Enrich.WithMachineName()
                .CreateLogger();

            int rowsCount = 50;

            for (int i = 0; i < rowsCount; i++)
            {
                logger.Information("Test{testNo}: {@testObject} test2: {@testObj2} testStr: {@testStr:l}", i, testObject, testObj2, "stringValue");
            }

            logger.Dispose();

            var actualRowsCount = _dbHelper.GetTableRowsCount(_tableName);

            Assert.Equal(rowsCount, actualRowsCount);
        }
コード例 #7
0
        public void Write50Events_ShouldInsert50EventsToDb()
        {
            _dbHelper.RemoveTable(_tableFullName);

            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };

            var testObj2 = new TestObjectType2 {
                DateProp1 = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>
            {
                { "message", new RenderedMessageColumnWriter() },
                { "message_template", new MessageTemplateColumnWriter() },
                { "level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
                { "raise_date", new TimestampColumnWriter() },
                { "exception", new ExceptionColumnWriter() },
                { "properties", new LogEventSerializedColumnWriter() },
                { "props_test", new PropertiesColumnWriter(NpgsqlDbType.Text) },
                { "machine_name", new SinglePropertyColumnWriter("MachineName") }
            };

            using (var logger =
                       new LoggerConfiguration().WriteTo.PostgreSQL(
                           _connectionString,
                           _tableName,
                           columnProps,
                           schemaName: _schemaName,
                           needAutoCreateTable: true)
                       .Enrich.WithMachineName()
                       .CreateLogger())
            {
                for (var i = 0; i < 50; i++)
                {
                    logger.Information("Test{testNo}: {@testObject} test2: {@testObj2}", i, testObject, testObj2);
                }
            }
            var rowsCount = _dbHelper.GetTableRowsCount(_tableFullName);

            Assert.Equal(50, rowsCount);
        }
コード例 #8
0
        public void Write50EventsShouldInsert50EventsToDb()
        {
            this.dbHelper.ClearTable(SchemaName, TableName);

            var testObject = new TestObjectType1 {
                IntProp = 42, StringProp = "Test"
            };

            var testObj2 = new TestObjectType2 {
                DateProp = DateTime.Now, NestedProp = testObject
            };

            var columnProps = new Dictionary <string, ColumnWriterBase>
            {
                { "Message", new RenderedMessageColumnWriter() },
                { "MessageTemplate", new MessageTemplateColumnWriter() },
                { "Level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
                { "RaiseDate", new TimestampColumnWriter() },
                { "Exception", new ExceptionColumnWriter() },
                { "Properties", new LogEventSerializedColumnWriter() },
                { "PropertyTest", new PropertiesColumnWriter(NpgsqlDbType.Text) },
                { "MachineName", new SinglePropertyColumnWriter("MachineName") }
            };

            var logger = new LoggerConfiguration().WriteTo
                         .PostgreSql(ConnectionString, TableName, columnProps, schemaName: SchemaName).Enrich.WithMachineName()
                         .CreateLogger();

            for (var i = 0; i < 50; i++)
            {
                logger.Information("Test{testNo}: {@testObject} test2: {@testObj2}", i, testObject, testObj2);
            }

            logger.Dispose();

            var rowsCount = this.dbHelper.GetTableRowsCount(SchemaName, TableName);

            Assert.Equal(50, rowsCount);
        }