serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
444 stars 129 forks source link

Serilog is not able to create database with AutoCreateSqlDatabase when reading configuration from appsettings. #421

Closed SanglierF closed 3 months ago

SanglierF commented 3 months ago

While using below code in Program.cs to use serilog, it doesn't want to create a database if it can't find one. And fails with:

Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "db_name_logs" requested by the login. The login failed.

var localSerilogReader = new ConfigurationReaderOptions { SectionName = "LocalSerilog" };

builder.Host.UseSerilog(
      (context, configuration) =>
                {
                    configuration.ReadFrom.Configuration(context.Configuration, localSerilogReader);
                });

But it works correctly when specifying AutoCreateSqlDatabase without using appsettings:

 configuration.WriteTo.MSSqlServer(
                      connectionString:
                      "Data Source=(localdb)\\MSSQLLocalDB; Database=db_name_logs;Trusted_Connection=True;",
                      sinkOptions: new MSSqlServerSinkOptions
                         {
                             AutoCreateSqlDatabase = true,
                             AutoCreateSqlTable = true,
                             TableName = "LogEvents",
                             SchemaName = "dbo",
                         });  

Appsettings file looks like this.

"LocalSerilog": {
        "Using": [
            "Serilog.Sinks.Console",
            "Serilog.Sinks.MSSqlServer"
        ],
        "MinimumLevel": {
            "Default": "Debug"
        },
        "WriteTo": [
            {
                "Name": "Console",
                "Args": {
                    "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
                    "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
                }
            },
            {
                "Name": "MSSqlServer",
                "Args": {
                    "ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB; Database=db_name_logs;Trusted_Connection=True;",
                    "AutoCreateSqlDatabase": true,
                    "AutoCreateSqlTable": true,
                    "SchemaName": "dbo",
                    "TableName": "LogEventsApi",
                    "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
                }
            }
        ]
    },

.Net Sdk - Version 8.0.5, released May 14, 2024 NuGet versions: Serilog - 3.1.1 Serilog.Sinks.MSSqlServer - 6.6.0 Serilog.Settings.Configuration - 8.0.0

SanglierF commented 3 months ago

My mistake - appsettings were incorrectly formatted: Did it exactly like shown in serilog.sink.mssql and it works now.

"Name": "MSSqlServer",
                "Args": {
                    "ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB; Database=db_dev_logs;Trusted_Connection=True;",
                    "sinkOptionsSection": {
                        "AutoCreateSqlDatabase": "true",
                        "AutoCreateSqlTable": "true",
                        "SchemaName": "dbo",
                        "TableName": "LogEventsFrontend",
                        "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
                    }
                }