﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Serilog.Sinks.MSSqlServer.Tests
{
    class TestPropertiesColumnFiltering
    {
    }
}

﻿using Dapper;
using FluentAssertions;
using System.Data.SqlClient;
using Xunit;

namespace Serilog.Sinks.MSSqlServer.Tests
{
    [Collection("LogTest")]
    public class TestPropertiesColumnFiltering
    {
        internal class PropertiesColumns
        {
            public string Properties { get; set; }
        }

        [Fact]
        public void FilteredProperties()
        {
            // arrange
            var columnOptions = new ColumnOptions();
            columnOptions.Properties.PropertiesFilter = (propName) => propName == "A";

            Log.Logger = new LoggerConfiguration()
                .WriteTo.MSSqlServer
                (
                    connectionString: DatabaseFixture.LogEventsConnectionString,
                    tableName: DatabaseFixture.LogTableName,
                    columnOptions: columnOptions,
                    autoCreateSqlTable: true
                )
                .CreateLogger();

            // act
            Log.Logger
                .ForContext("A", "AValue")
                .ForContext("B", "BValue")
                .Information("Logging message");

            Log.CloseAndFlush();

            // assert
            using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
            {
                var logEvents = conn.Query<PropertiesColumns>($"SELECT Properties from {DatabaseFixture.LogTableName}");

                logEvents.Should().Contain(e => e.Properties.Contains("AValue"));
                logEvents.Should().NotContain(e => e.Properties.Contains("BValue"));
            }
        }
    }
}

Test for properties column filtering
Test for properties column filtering
