Skip to content

Commit e223115

Browse files
committed
Small adjustments
1 parent 9edb6e2 commit e223115

File tree

5 files changed

+152
-164
lines changed

5 files changed

+152
-164
lines changed

src/Serilog.Sinks.MSSqlServer/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ public static LoggerConfiguration MSSqlServer(
6464

6565
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
6666

67-
MSSqlServerConfigurationSection serviceConfigSection =
68-
ConfigurationManager.GetSection("MSSqlServerSettingsSection") as MSSqlServerConfigurationSection;
69-
7067
// If we have additional columns from config, load them as well
71-
if (serviceConfigSection != null && serviceConfigSection.Columns.Count > 0)
68+
if (ConfigurationManager.GetSection("MSSqlServerSettingsSection") is MSSqlServerConfigurationSection serviceConfigSection && serviceConfigSection.Columns.Count > 0)
7269
{
7370
if (columnOptions == null)
7471
{
@@ -120,11 +117,8 @@ public static LoggerConfiguration MSSqlServer(this LoggerAuditSinkConfiguration
120117
{
121118
if (loggerAuditSinkConfiguration == null) throw new ArgumentNullException("loggerAuditSinkConfiguration");
122119

123-
MSSqlServerConfigurationSection serviceConfigSection =
124-
ConfigurationManager.GetSection("MSSqlServerSettingsSection") as MSSqlServerConfigurationSection;
125-
126120
// If we have additional columns from config, load them as well
127-
if (serviceConfigSection != null && serviceConfigSection.Columns.Count > 0)
121+
if (ConfigurationManager.GetSection("MSSqlServerSettingsSection") is MSSqlServerConfigurationSection serviceConfigSection && serviceConfigSection.Columns.Count > 0)
128122
{
129123
if (columnOptions == null)
130124
{

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerAuditSink.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public MSSqlServerAuditSink(
5454
if (columnOptions.DisableTriggers)
5555
throw new NotSupportedException($"The {nameof(ColumnOptions.DisableTriggers)} option is not supported for auditing.");
5656
}
57-
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, autoCreateSqlTable);
58-
5957

58+
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, autoCreateSqlTable);
59+
6060
}
6161

6262
/// <summary>Emit the provided log event to the sink.</summary>

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSinkTraits.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,8 @@ private IEnumerable<KeyValuePair<string, object>> ConvertPropertiesToColumn(IRea
194194

195195
var columnName = property.Key;
196196
var columnType = EventTable.Columns[columnName].DataType;
197-
object conversion;
198197

199-
var scalarValue = property.Value as ScalarValue;
200-
if (scalarValue == null)
198+
if (!(property.Value is ScalarValue scalarValue))
201199
{
202200
yield return new KeyValuePair<string, object>(columnName, property.Value.ToString());
203201
continue;
@@ -209,7 +207,7 @@ private IEnumerable<KeyValuePair<string, object>> ConvertPropertiesToColumn(IRea
209207
continue;
210208
}
211209

212-
if (TryChangeType(scalarValue.Value, columnType, out conversion))
210+
if (TryChangeType(scalarValue.Value, columnType, out var conversion))
213211
{
214212
yield return new KeyValuePair<string, object>(columnName, conversion);
215213
}

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

Lines changed: 142 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -5,152 +5,152 @@
55

66
namespace Serilog.Sinks.MSSqlServer
77
{
8-
internal class SqlTableCreator
9-
{
10-
private readonly string _connectionString;
11-
private string _tableName;
12-
private string _schemaName;
13-
14-
#region Constructor
15-
16-
public SqlTableCreator(string connectionString, string schemaName)
17-
{
18-
_schemaName = schemaName;
19-
_connectionString = connectionString;
20-
}
21-
22-
#endregion
23-
24-
#region Instance Methods
25-
public int CreateTable(DataTable table)
26-
{
27-
if (table == null) return 0;
28-
29-
if (string.IsNullOrWhiteSpace(table.TableName) || string.IsNullOrWhiteSpace(_connectionString)) return 0;
30-
31-
_tableName = table.TableName;
32-
using (var conn = new SqlConnection(_connectionString))
33-
{
34-
string sql = GetSqlFromDataTable(_tableName, table, _schemaName);
35-
using (SqlCommand cmd = new SqlCommand(sql, conn))
36-
{
37-
conn.Open();
38-
return cmd.ExecuteNonQuery();
39-
}
40-
41-
}
42-
}
43-
#endregion
44-
45-
#region Static Methods
46-
47-
private static string GetSqlFromDataTable(string tableName, DataTable table, string schema)
48-
{
49-
StringBuilder sql = new StringBuilder();
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);
51-
sql.AppendLine(" BEGIN");
52-
sql.AppendFormat(" CREATE TABLE [{0}].[{1}] ( ", schema, tableName);
53-
54-
// columns
55-
int numOfColumns = table.Columns.Count;
56-
int i = 1;
57-
foreach (DataColumn column in table.Columns)
58-
{
59-
sql.AppendFormat("[{0}] {1}", column.ColumnName, SqlGetType(column));
60-
if (column.ColumnName.ToUpper().Equals("ID") || column.AutoIncrement)
61-
sql.Append(" IDENTITY(1,1) ");
62-
if (numOfColumns > i)
63-
sql.AppendFormat(", ");
64-
i++;
65-
}
66-
67-
// primary keys
68-
if (table.PrimaryKey.Length > 0)
69-
{
70-
sql.AppendFormat(" CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED (", tableName);
71-
72-
int numOfKeys = table.PrimaryKey.Length;
73-
i = 1;
74-
foreach (DataColumn column in table.PrimaryKey)
75-
{
76-
sql.AppendFormat("[{0}]", column.ColumnName);
77-
if (numOfKeys > i)
78-
sql.AppendFormat(", ");
79-
80-
i++;
81-
}
82-
sql.Append("))");
83-
}
84-
sql.AppendLine(" END");
85-
return sql.ToString();
86-
}
87-
88-
// Return T-SQL data type definition, based on schema definition for a column
89-
private static string SqlGetType(object type, int columnSize, int numericPrecision, int numericScale,
90-
bool allowDbNull)
91-
{
92-
string sqlType;
93-
94-
switch (type.ToString())
95-
{
96-
case "System.Boolean":
97-
sqlType = "BIT";
98-
break;
8+
internal class SqlTableCreator
9+
{
10+
private readonly string _connectionString;
11+
private string _tableName;
12+
private string _schemaName;
13+
14+
#region Constructor
15+
16+
public SqlTableCreator(string connectionString, string schemaName)
17+
{
18+
_schemaName = schemaName;
19+
_connectionString = connectionString;
20+
}
21+
22+
#endregion
23+
24+
#region Instance Methods
25+
public int CreateTable(DataTable table)
26+
{
27+
if (table == null) return 0;
28+
29+
if (string.IsNullOrWhiteSpace(table.TableName) || string.IsNullOrWhiteSpace(_connectionString)) return 0;
30+
31+
_tableName = table.TableName;
32+
using (var conn = new SqlConnection(_connectionString))
33+
{
34+
string sql = GetSqlFromDataTable(_tableName, table, _schemaName);
35+
using (SqlCommand cmd = new SqlCommand(sql, conn))
36+
{
37+
conn.Open();
38+
return cmd.ExecuteNonQuery();
39+
}
40+
41+
}
42+
}
43+
#endregion
44+
45+
#region Static Methods
46+
47+
private static string GetSqlFromDataTable(string tableName, DataTable table, string schema)
48+
{
49+
StringBuilder sql = new StringBuilder();
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);
51+
sql.AppendLine(" BEGIN");
52+
sql.AppendFormat(" CREATE TABLE [{0}].[{1}] ( ", schema, tableName);
53+
54+
// columns
55+
int numOfColumns = table.Columns.Count;
56+
int i = 1;
57+
foreach (DataColumn column in table.Columns)
58+
{
59+
sql.AppendFormat("[{0}] {1}", column.ColumnName, SqlGetType(column));
60+
if (column.ColumnName.ToUpper().Equals("ID") || column.AutoIncrement)
61+
sql.Append(" IDENTITY(1,1) ");
62+
if (numOfColumns > i)
63+
sql.AppendFormat(", ");
64+
i++;
65+
}
66+
67+
// primary keys
68+
if (table.PrimaryKey.Length > 0)
69+
{
70+
sql.AppendFormat(" CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED (", tableName);
71+
72+
int numOfKeys = table.PrimaryKey.Length;
73+
i = 1;
74+
foreach (DataColumn column in table.PrimaryKey)
75+
{
76+
sql.AppendFormat("[{0}]", column.ColumnName);
77+
if (numOfKeys > i)
78+
sql.AppendFormat(", ");
79+
80+
i++;
81+
}
82+
sql.Append("))");
83+
}
84+
sql.AppendLine(" END");
85+
return sql.ToString();
86+
}
87+
88+
// Return T-SQL data type definition, based on schema definition for a column
89+
private static string SqlGetType(object type, int columnSize, int numericPrecision, int numericScale,
90+
bool allowDbNull)
91+
{
92+
string sqlType;
93+
94+
switch (type.ToString())
95+
{
96+
case "System.Boolean":
97+
sqlType = "BIT";
98+
break;
9999

100100
case "System.Byte":
101101
sqlType = "TINYINT";
102102
break;
103103

104104
case "System.String":
105-
sqlType = "NVARCHAR(" + ((columnSize == -1) ? "MAX" : columnSize.ToString()) + ")";
106-
break;
107-
108-
case "System.Decimal":
109-
if (numericScale > 0)
110-
sqlType = "REAL";
111-
else if (numericPrecision > 10)
112-
sqlType = "BIGINT";
113-
else
114-
sqlType = "INT";
115-
break;
116-
117-
case "System.Double":
118-
case "System.Single":
119-
sqlType = "REAL";
120-
break;
121-
122-
case "System.Int64":
123-
sqlType = "BIGINT";
124-
break;
125-
126-
case "System.Int16":
127-
case "System.Int32":
128-
sqlType = "INT";
129-
break;
130-
131-
case "System.DateTime":
132-
sqlType = "DATETIME";
133-
break;
134-
135-
case "System.Guid":
136-
sqlType = "UNIQUEIDENTIFIER";
137-
break;
138-
139-
default:
140-
throw new Exception(string.Format("{0} not implemented.", type));
141-
}
142-
143-
sqlType += " " + (allowDbNull ? "NULL" : "NOT NULL");
144-
145-
return sqlType;
146-
}
147-
148-
// Overload based on DataColumn from DataTable type
149-
private static string SqlGetType(DataColumn column)
150-
{
151-
return SqlGetType(column.DataType, column.MaxLength, 10, 2, column.AllowDBNull);
152-
}
153-
154-
#endregion
155-
}
105+
sqlType = "NVARCHAR(" + ((columnSize == -1) ? "MAX" : columnSize.ToString()) + ")";
106+
break;
107+
108+
case "System.Decimal":
109+
if (numericScale > 0)
110+
sqlType = "REAL";
111+
else if (numericPrecision > 10)
112+
sqlType = "BIGINT";
113+
else
114+
sqlType = "INT";
115+
break;
116+
117+
case "System.Double":
118+
case "System.Single":
119+
sqlType = "REAL";
120+
break;
121+
122+
case "System.Int64":
123+
sqlType = "BIGINT";
124+
break;
125+
126+
case "System.Int16":
127+
case "System.Int32":
128+
sqlType = "INT";
129+
break;
130+
131+
case "System.DateTime":
132+
sqlType = "DATETIME";
133+
break;
134+
135+
case "System.Guid":
136+
sqlType = "UNIQUEIDENTIFIER";
137+
break;
138+
139+
default:
140+
throw new Exception(string.Format("{0} not implemented.", type));
141+
}
142+
143+
sqlType += " " + (allowDbNull ? "NULL" : "NOT NULL");
144+
145+
return sqlType;
146+
}
147+
148+
// Overload based on DataColumn from DataTable type
149+
private static string SqlGetType(DataColumn column)
150+
{
151+
return SqlGetType(column.DataType, column.MaxLength, 10, 2, column.AllowDBNull);
152+
}
153+
154+
#endregion
155+
}
156156
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/XmlPropertyFormatter.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ public static class XmlPropertyFormatter
4545
/// <returns>A simplified representation.</returns>
4646
public static string Simplify(LogEventPropertyValue value, ColumnOptions.PropertiesColumnOptions options)
4747
{
48-
var scalar = value as ScalarValue;
49-
if (scalar != null)
48+
if (value is ScalarValue scalar)
5049
return SimplifyScalar(scalar.Value);
5150

52-
var dict = value as DictionaryValue;
53-
if (dict != null)
51+
if (value is DictionaryValue dict)
5452
{
5553
var sb = new StringBuilder();
5654

@@ -92,8 +90,7 @@ public static string Simplify(LogEventPropertyValue value, ColumnOptions.Propert
9290
return sb.ToString();
9391
}
9492

95-
var seq = value as SequenceValue;
96-
if (seq != null)
93+
if (value is SequenceValue seq)
9794
{
9895
var sb = new StringBuilder();
9996

@@ -127,8 +124,7 @@ public static string Simplify(LogEventPropertyValue value, ColumnOptions.Propert
127124
return sb.ToString();
128125
}
129126

130-
var str = value as StructureValue;
131-
if (str != null)
127+
if (value is StructureValue str)
132128
{
133129
var props = str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value, options));
134130

0 commit comments

Comments
 (0)