serilog-mssql / serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
Apache License 2.0
283 stars 148 forks source link

The Id column isn't changing the datatype to UniqueIdentifier in configuration #593

Closed DM-98 closed 23 hours ago

DM-98 commented 3 weeks ago

Please clearly describe what the SQL Sink is doing incorrectly: It creates the table, but ignoring my setup which is to change "Id" column to a UniqueIdentifier.

Please clearly describe the expected behavior: I expect to change the "Id" datatype to UniqueIdentifier instead of Int.

List the names and versions of all Serilog packages used in the project:

cancakar35 commented 2 weeks ago

The Id column is default, this is why you are getting integer column. This code should help. However i strongly recommend using standard Id column as primary key. If you have any concerns, you can use BigInt type for large scale apps.

var colOpts = new ColumnOptions();
colOpts.Store.Remove(StandardColumn.Id);
SqlColumn customIdColumn = new("Id", System.Data.SqlDbType.UniqueIdentifier, allowNull: false);
colOpts.AdditionalColumns =[customIdColumn];
colOpts.PrimaryKey = customIdColumn;

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .WriteTo.MSSqlServer(connectionString: "your_conn_str",
    sinkOptions: new MSSqlServerSinkOptions { AutoCreateSqlDatabase = false, AutoCreateSqlTable = false, TableName = "Logs" }, columnOptions: colOpts)
    //.Enrich.With<YourCustomSerilogEnricher>() use enricher to fill id field
    .CreateLogger();
ckadluba commented 1 week ago

Hi @DM-98!

Did the solution suggested by @cancakar35 work for you?

ckadluba commented 23 hours ago

Closing issue due to inactivity.