Skip to content

Commit 72609ff

Browse files
authored
Merge pull request #77 from serilog/dev
5.0.0 Release
2 parents d6b5bec + a2fe9d6 commit 72609ff

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/Serilog.Sinks.MSSqlServer/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
3838
/// <param name="loggerConfiguration">The logger configuration.</param>
3939
/// <param name="connectionString">The connection string to the database where to store the events.</param>
4040
/// <param name="tableName">Name of the table to store the events in.</param>
41+
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
4142
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
4243
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
4344
/// <param name="period">The time to wait between checking for event batches.</param>
@@ -55,7 +56,8 @@ public static LoggerConfiguration MSSqlServer(
5556
TimeSpan? period = null,
5657
IFormatProvider formatProvider = null,
5758
bool autoCreateSqlTable = false,
58-
ColumnOptions columnOptions = null
59+
ColumnOptions columnOptions = null,
60+
string schemaName = "dbo"
5961
)
6062
{
6163
if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration");
@@ -85,7 +87,8 @@ public static LoggerConfiguration MSSqlServer(
8587
defaultedPeriod,
8688
formatProvider,
8789
autoCreateSqlTable,
88-
columnOptions
90+
columnOptions,
91+
schemaName
8992
),
9093
restrictedToMinimumLevel);
9194
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class MSSqlServerSink : PeriodicBatchingSink
4848
readonly DataTable _eventsTable;
4949
readonly IFormatProvider _formatProvider;
5050
readonly string _tableName;
51+
readonly string _schemaName;
5152
private readonly ColumnOptions _columnOptions;
5253

5354
private readonly HashSet<string> _additionalDataColumnNames;
@@ -60,6 +61,7 @@ public class MSSqlServerSink : PeriodicBatchingSink
6061
/// </summary>
6162
/// <param name="connectionString">Connection string to access the database.</param>
6263
/// <param name="tableName">Name of the table to store the data in.</param>
64+
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
6365
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
6466
/// <param name="period">The time to wait between checking for event batches.</param>
6567
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
@@ -72,7 +74,8 @@ public MSSqlServerSink(
7274
TimeSpan period,
7375
IFormatProvider formatProvider,
7476
bool autoCreateSqlTable = false,
75-
ColumnOptions columnOptions = null
77+
ColumnOptions columnOptions = null,
78+
string schemaName = "dbo"
7679
)
7780
: base(batchPostingLimit, period)
7881
{
@@ -84,6 +87,7 @@ public MSSqlServerSink(
8487

8588
_connectionString = connectionString;
8689
_tableName = tableName;
90+
_schemaName = schemaName;
8791
_formatProvider = formatProvider;
8892
_columnOptions = columnOptions ?? new ColumnOptions();
8993
if (_columnOptions.AdditionalDataColumns != null)
@@ -99,7 +103,7 @@ public MSSqlServerSink(
99103
{
100104
try
101105
{
102-
SqlTableCreator tableCreator = new SqlTableCreator(connectionString);
106+
SqlTableCreator tableCreator = new SqlTableCreator(connectionString, _schemaName);
103107
tableCreator.CreateTable(_eventsTable);
104108
}
105109
catch (Exception ex)
@@ -134,7 +138,7 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
134138
: new SqlBulkCopy(cn, SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.FireTriggers, null)
135139
)
136140
{
137-
copy.DestinationTableName = _tableName;
141+
copy.DestinationTableName = string.Format("[{0}].[{1}]", _schemaName, _tableName);
138142
foreach (var column in _eventsTable.Columns)
139143
{
140144
var columnName = ((DataColumn)column).ColumnName;

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlTableCreator.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ internal class SqlTableCreator
99
{
1010
private readonly string _connectionString;
1111
private string _tableName;
12+
private string _schemaName;
1213

1314
#region Constructor
1415

15-
public SqlTableCreator(string connectionString)
16+
public SqlTableCreator(string connectionString, string schemaName)
1617
{
17-
_connectionString = connectionString;
18+
_schemaName = schemaName;
19+
_connectionString = connectionString;
1820
}
1921

2022
#endregion
@@ -29,7 +31,7 @@ public int CreateTable(DataTable table)
2931
_tableName = table.TableName;
3032
using (var conn = new SqlConnection(_connectionString))
3133
{
32-
string sql = GetSqlFromDataTable(_tableName, table);
34+
string sql = GetSqlFromDataTable(_tableName, table, _schemaName);
3335
using (SqlCommand cmd = new SqlCommand(sql, conn))
3436
{
3537
conn.Open();
@@ -42,12 +44,12 @@ public int CreateTable(DataTable table)
4244

4345
#region Static Methods
4446

45-
private static string GetSqlFromDataTable(string tableName, DataTable table)
47+
private static string GetSqlFromDataTable(string tableName, DataTable table, string schema)
4648
{
4749
StringBuilder sql = new StringBuilder();
48-
sql.AppendFormat("IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = '{0}' AND xtype = 'U')", tableName);
50+
sql.AppendFormat("IF NOT EXISTS (SELECT s.name, t.name FROM sys.tables t JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE s.name = '{0}' AND t.name = '{1}')", schema, tableName);
4951
sql.AppendLine(" BEGIN");
50-
sql.AppendFormat(" CREATE TABLE [{0}] ( ", tableName);
52+
sql.AppendFormat(" CREATE TABLE [{0}].[{1}] ( ", schema, tableName);
5153

5254
// columns
5355
int numOfColumns = table.Columns.Count;

src/Serilog.Sinks.MSSqlServer/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "4.2.0-*",
2+
"version": "5.0.0-*",
33
"description": "A Serilog sink that writes events to Microsoft SQL Server",
44
"authors": [
55
"Michiel van Oudheusden",

0 commit comments

Comments
 (0)