serilog-sinks-mssqlserver:在xml配置文件中排除列

时间:2017-12-21 20:35:34

标签: c# serilog

如何排除xml配置文件中的列?

我想这样做serilogColumnOptions.Store.Remove(StandardColumn.Properties);配置文件中的事件

1 个答案:

答案 0 :(得分:2)

似乎无法通过XML配置删除Serilog列。

我发现了以下声明的几个确认:herehere

您还可以通过检查SQL Server Sink的source code来验证这一点:

MSSqlServer()扩展方法检查MSSqlServerSettingsSection部分,以配置documentation中描述的列。

MSSqlServerConfigurationSection serviceConfigSection =
    ConfigurationManager.GetSection("MSSqlServerSettingsSection") as MSSqlServerConfigurationSection;

// If we have additional columns from config, load them as well
if (serviceConfigSection != null && serviceConfigSection.Columns.Count > 0)
{
    if (columnOptions == null)
    {
        columnOptions = new ColumnOptions();
    }
    GenerateDataColumnsFromConfig(serviceConfigSection, columnOptions);
}

如果您接着检查GenerateDataColumnsFromConfig()方法,则会看到所有已配置的列都已添加到AdditionalDataColumns ColumnOptions的{​​{1}}集合中。 但是没有任何代码可以从Store集合中删除列。

如果您的应用程序必须具有Seri​​log的文件配置并且有可能删除某些列,则可以从文件中添加简单替换Serilog配置。以下是此类配置的示例实现:

var connectionString = ConfigurationManager.AppSettings["serilog:write-to:MSSqlServer.connectionString"];
var tableName = ConfigurationManager.AppSettings["serilog:write-to:MSSqlServer.tableName"];
var autoCreateSqlTable = Convert.ToBoolean(ConfigurationManager.AppSettings["serilog:write-to:MSSqlServer.autoCreateSqlTable"]);
var excludedColumns = ConfigurationManager.AppSettings["serilog:write-to:MSSqlServer.excludedColumns"];

ColumnOptions columnOptions = new ColumnOptions();
foreach (var excludedColumn in Regex.Split(excludedColumns, ",\\s*"))
{
    columnOptions.Store.Remove((StandardColumn)Enum.Parse(typeof(StandardColumn), excludedColumn, true));
}

Logger log = new LoggerConfiguration()
    .WriteTo.MSSqlServer(connectionString, tableName, columnOptions: columnOptions, autoCreateSqlTable: autoCreateSqlTable)
    .CreateLogger();

以下是appSettings部分:

  <appSettings>
    <add key="serilog:using:MSSqlSever" value="Serilog.Sinks.MSSqlServer" />
    <add key="serilog:write-to:MSSqlServer.connectionString" value="Server=.;Database=LoggingDB;Trusted_Connection=True;"/>
    <add key="serilog:write-to:MSSqlServer.tableName" value="Logs"/>
    <add key="serilog:write-to:MSSqlServer.autoCreateSqlTable" value="true"/>
    <add key="serilog:write-to:MSSqlServer.excludedColumns" value="Properties, TimeStamp"/>
  </appSettings>

希望这会对你有所帮助。