mustafa00
mustafa00

Reputation: 891

How to configure App.config to remove Serilog.Sinks.MSSqlServer standard columns?

I use Serilog for logging. I need to configure App.config as to remove 2 standard columns from Logs table (StandardColumn.Properties and StandardColumn.MessageTemplate). I've searched for it in Serilog docs and other resources but can't find how to do it. This is what I got so far:

<configuration>
    <appSettings>
        <add key="serilog:using:MSSqlServer" value="Serilog.Sinks.MSSqlServer" />
        <add key="serilog:write-to:MSSqlServer.connectionString" value="Data Source=SomeServer;initial catalog=DB; integrated security=True" />
        <add key="serilog:write-to:MSSqlServer.tableName" value="Logs" />
        <add key="serilog:minimum-level" value="Debug" />
    </appSettings>
</configuration>

I guess I would have to add this line:

<add key="serilog:write-to:MSSqlServer.columnOptions" value="someColOptions" />

but how should I define someColOptions? For example, I removed columns using this kind of code:

var colOptions = new Serilog.Sinks.MSSqlServer.ColumnOptions();
colOptions.Store.Remove(StandardColumn.Properties);
colOptions.Store.Remove(StandardColumn.MessageTemplate);

Log.Logger = new LoggerConfiguration()
    .WriteTo
    .MSSqlServer(
        connectionString: conn_string,
        sinkOptions: new MSSqlServerSinkOptions { TableName = "Logs" },
        columnOptions: colOptions)
    .MinimumLevel.Debug()
    .CreateLogger();

and now I want to do the same in App.config

Upvotes: 1

Views: 1666

Answers (1)

Phil Bevan
Phil Bevan

Reputation: 56

I had a similar question, I've been able to find out that by adding the following, the configSections has to be first section after that this will:

  1. create a Logs table in the specified Database (from the connection string)
  2. Use the Serilog database account (from the connection string)
  3. Remove the column called Properties from the Logs table
  4. Add a new column called RunId that declared as an int
<configuration>
    <configSections>
        <section name="MSSqlServerSettingsSection" type="Serilog.Configuration.MSSqlServerConfigurationSection, Serilog.Sinks.MSSqlServer"/>
    </configSections>
    <MSSqlServerSettingsSection DisableTriggers="false" ClusteredColumnstoreIndex="false" PrimaryKeyColumnName="Id">
        <!-- SinkOptions parameters -->
        <TableName Value="Logs"/>
        <BatchPeriod Value="00:00:15"/>
        <RemoveStandardColumns>
            <remove Name="Properties"/>
        </RemoveStandardColumns>        
        <Columns>
            <add ColumnName="RunId" DataType="int"/>
        </Columns>
    </MSSqlServerSettingsSection>
    <appSettings>
        <!-- Serilog Settings -->
        <add key="serilog:minimum-level" value="Verbose" />
        <add key="serilog:using:Console" value="Serilog.Sinks.Console"/>
        <add key="serilog:write-to:Console"/>
        <add key="serilog:using:File" value="Serilog.Sinks.File" />
        <add key="serilog:write-to:File"/>
        <add key="serilog:write-to:File.path" value="{PATH}" />
        <add key="serilog:write-to:File.rollingInterval" value="Day"/>
        <add key="serilog:using:MSSqlServer" value="Serilog.Sinks.MSSqlServer" />
        <add key="serilog:write-to:MSSqlServer.connectionString" value="Server=[SERVER];Database=[DATABASE];User Id=Serilog;Password=[PASSWORD];"/>
        <add key="serilog:write-to:MSSqlServer.tableName" value="Logs"/>
        <add key="serilog:write-to:MSSqlServer.autoCreateSqlTable" value="true"/>
    </appSettings>
</configuration>

Calling Log.("{RunId}{Message}", _runId, _messageToShow) causes my File, Console AND MSSqlServer sinks to execute writing to these, if it doesn't exist the Logs table will be created, removing the Properties column and adding the RunId column. (If the table exists it doesn't do anything but write the data, still trying to work out how to handle that).

I'm still playing around with this (my next thing is to work out how to write {RunId} but not have it appear in the log, only written to the corresponding database field).

HTH,

Phil

Upvotes: 1

Related Questions