From aef964a138a068f568bc6580c253a51a9b6653f6 Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:28:46 +0200 Subject: [PATCH 1/2] Fixed issue #478 Do not throw or log an exception if AutoCreateSqlDatabase is set to true and the DB alerady exists. --- .../MSSqlServer/Platform/SqlCreateDatabaseWriter.cs | 12 +++++++++++- .../Platform/SqlCreateDatabaseWriterTests.cs | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriter.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriter.cs index bdee32e5..734c5f2a 100644 --- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriter.cs +++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriter.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using static System.FormattableString; namespace Serilog.Sinks.MSSqlServer.Platform @@ -15,6 +16,15 @@ public SqlCreateDatabaseWriter(string databaseName) public string DatabaseName => _databaseName; public string GetSql() - => Invariant($"CREATE DATABASE [{_databaseName}]"); + { + var sql = new StringBuilder(); + + sql.AppendLine(Invariant($"IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = '{_databaseName}')")); + sql.AppendLine("BEGIN"); + sql.AppendLine(Invariant($"CREATE DATABASE [{_databaseName}]")); + sql.AppendLine("END"); + + return sql.ToString(); + } } } diff --git a/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriterTests.cs b/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriterTests.cs index 5766e69d..27931b56 100644 --- a/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriterTests.cs +++ b/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCreateDatabaseWriterTests.cs @@ -12,7 +12,7 @@ public void GetSqlWritesCorrectCommand() { // Arrange const string databaseName = "LogDatabase"; - const string expectedResult = "CREATE DATABASE [LogDatabase]"; + const string expectedResult = "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'LogDatabase')\r\nBEGIN\r\nCREATE DATABASE [LogDatabase]\r\nEND\r\n"; var sut = new SqlCreateDatabaseWriter(databaseName); // Act @@ -27,7 +27,7 @@ public void GetSqlWritesCorrectCommandForDatabaseNameWithSpaces() { // Arrange const string databaseName = "Log Data Base"; - const string expectedResult = "CREATE DATABASE [Log Data Base]"; + const string expectedResult = "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'Log Data Base')\r\nBEGIN\r\nCREATE DATABASE [Log Data Base]\r\nEND\r\n"; var sut = new SqlCreateDatabaseWriter(databaseName); // Act From 12c265e4b86328921e2cd30f7e1f819f2d2e5432 Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:55:01 +0200 Subject: [PATCH 2/2] Throw sink init exceptions Currently exceptions that occurr during automatic db or table creation when the sink is initialized, are not thrown to the caller/client program. This could hide or delay fatal problems from showing and was changed in this commit. Related: #478 --- .../Sinks/MSSqlServer/Platform/SqlCommandExecutor.cs | 1 + .../Sinks/MSSqlServer/Platform/SqlCommandExecutorTests.cs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCommandExecutor.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCommandExecutor.cs index ce26cf35..8631ce12 100644 --- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCommandExecutor.cs +++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCommandExecutor.cs @@ -32,6 +32,7 @@ public void Execute() catch (Exception ex) { HandleException(ex); + throw; } } diff --git a/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCommandExecutorTests.cs b/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCommandExecutorTests.cs index 87efa8b4..6c39d45a 100644 --- a/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCommandExecutorTests.cs +++ b/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Platform/SqlCommandExecutorTests.cs @@ -100,7 +100,7 @@ public void HandlesExceptionFromSqlWriter() }; // Act - _sut.Execute(); + Assert.Throws(() => _sut.Execute()); // Assert Assert.True(handlerCalled); @@ -124,7 +124,7 @@ public void HandlesExceptionFromConnectionFactory() }; // Act - _sut.Execute(); + Assert.Throws(() => _sut.Execute()); // Assert Assert.True(handlerCalled); @@ -148,7 +148,7 @@ public void HandlesExceptionFromConnection() }; // Act - _sut.Execute(); + Assert.Throws(() => _sut.Execute()); // Assert Assert.True(handlerCalled); @@ -172,7 +172,7 @@ public void HandlesExceptionFromCommand() }; // Act - _sut.Execute(); + Assert.Throws(() => _sut.Execute()); // Assert Assert.True(handlerCalled);