如何排除xml配置文件中的列?
我想这样做serilogColumnOptions.Store.Remove(StandardColumn.Properties);配置文件中的事件
答案 0 :(得分:2)
似乎无法通过XML配置删除Serilog列。
您还可以通过检查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
集合中删除列。
如果您的应用程序必须具有Serilog的文件配置并且有可能删除某些列,则可以从文件中添加简单替换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>
希望这会对你有所帮助。