Skip to content

Commit 37e5186

Browse files
authored
Merge pull request #41 from colin-young/custom-column-names
Custom column names
2 parents c6dbc1a + 64ec915 commit 37e5186

File tree

2 files changed

+114
-107
lines changed

2 files changed

+114
-107
lines changed

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Serilog.Sinks.MSSqlServer
1010
/// </summary>
1111
public class ColumnOptions
1212
{
13-
private ICollection<StandardColumn> _store;
13+
ICollection<StandardColumn> _store;
1414

1515
/// <summary>
1616
/// Default constructor.
@@ -33,8 +33,10 @@ public ColumnOptions()
3333
StandardColumn.Properties
3434
};
3535

36+
Message = new MessageColumnOptions();
37+
MessageTemplate = new MessageTemplateColumnOptions();
3638
TimeStamp = new TimeStampColumnOptions();
37-
39+
Exception = new ExceptionColumnOptions();
3840
LogEvent = new LogEventColumnOptions();
3941
}
4042

@@ -49,7 +51,7 @@ public ICollection<StandardColumn> Store
4951
if (value == null)
5052
{
5153
_store = new Collection<StandardColumn>();
52-
foreach (StandardColumn column in Enum.GetValues(typeof (StandardColumn)))
54+
foreach (StandardColumn column in Enum.GetValues(typeof(StandardColumn)))
5355
{
5456
_store.Add(column);
5557
}
@@ -81,6 +83,21 @@ public ICollection<StandardColumn> Store
8183
/// </summary>
8284
public PropertiesColumnOptions Properties { get; private set; }
8385

86+
/// <summary>
87+
/// Options for the Exception column.
88+
/// </summary>
89+
public ExceptionColumnOptions Exception { get; set; }
90+
91+
/// <summary>
92+
/// Options for the MessageTemplate column.
93+
/// </summary>
94+
public MessageTemplateColumnOptions MessageTemplate { get; set; }
95+
96+
/// <summary>
97+
/// Options for the Message column.
98+
/// </summary>
99+
public MessageColumnOptions Message { get; set; }
100+
84101
/// <summary>
85102
/// Options for the TimeStamp column.
86103
/// </summary>
@@ -94,18 +111,12 @@ public ICollection<StandardColumn> Store
94111
/// <summary>
95112
/// Options for the Id column.
96113
/// </summary>
97-
public class IdColumnOptions
98-
{
99-
/// <summary>
100-
/// The name of the Id column. "Id" is used if not set.
101-
/// </summary>
102-
public string ColumnName { get; set; }
103-
}
114+
public class IdColumnOptions : CommonColumnOptions { }
104115

105116
/// <summary>
106117
/// Options for the Level column.
107118
/// </summary>
108-
public class LevelColumnOptions
119+
public class LevelColumnOptions : CommonColumnOptions
109120
{
110121
/// <summary>
111122
/// If true will store Level as an enum in a tinyint column as opposed to a string.
@@ -116,7 +127,7 @@ public class LevelColumnOptions
116127
/// <summary>
117128
/// Options for the Properties column.
118129
/// </summary>
119-
public class PropertiesColumnOptions
130+
public class PropertiesColumnOptions : CommonColumnOptions
120131
{
121132
/// <summary>
122133
/// Default constructor.
@@ -193,10 +204,21 @@ public PropertiesColumnOptions()
193204
public bool UsePropertyKeyAsElementName { get; set; }
194205
}
195206

207+
/// <summary>
208+
/// Shared column customization options.
209+
/// </summary>
210+
public class CommonColumnOptions
211+
{
212+
/// <summary>
213+
/// The name of the column in the database.
214+
/// </summary>
215+
public string ColumnName { get; set; }
216+
}
217+
196218
/// <summary>
197219
/// Options for the TimeStamp column.
198220
/// </summary>
199-
public class TimeStampColumnOptions
221+
public class TimeStampColumnOptions : CommonColumnOptions
200222
{
201223
/// <summary>
202224
/// If true, the time is converted to universal time.
@@ -207,12 +229,27 @@ public class TimeStampColumnOptions
207229
/// <summary>
208230
/// Options for the LogEvent column.
209231
/// </summary>
210-
public class LogEventColumnOptions
232+
public class LogEventColumnOptions : CommonColumnOptions
211233
{
212234
/// <summary>
213235
/// Exclude properties from the LogEvent column if they are being saved to additional columns.
214236
/// </summary>
215237
public bool ExcludeAdditionalProperties { get; set; }
216238
}
239+
240+
/// <summary>
241+
/// Options for the message column
242+
/// </summary>
243+
public class MessageColumnOptions : CommonColumnOptions {}
244+
245+
/// <summary>
246+
/// Options for the Exception column.
247+
/// </summary>
248+
public class ExceptionColumnOptions : CommonColumnOptions {}
249+
250+
/// <summary>
251+
/// Options for the MessageTemplate column.
252+
/// </summary>
253+
public class MessageTemplateColumnOptions : CommonColumnOptions {}
217254
}
218-
}
255+
}

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

Lines changed: 62 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -166,90 +166,66 @@ DataTable CreateDataTable()
166166
};
167167
eventsTable.Columns.Add(id);
168168

169-
if (_columnOptions.Store.Contains(StandardColumn.Message))
169+
foreach (var standardColumn in _columnOptions.Store)
170170
{
171-
var message = new DataColumn
171+
switch (standardColumn)
172172
{
173-
DataType = typeof (string),
174-
MaxLength = -1,
175-
ColumnName = "Message"
176-
};
177-
eventsTable.Columns.Add(message);
178-
}
179-
180-
if (_columnOptions.Store.Contains(StandardColumn.MessageTemplate))
181-
{
182-
var messageTemplate = new DataColumn
183-
{
184-
DataType = typeof (string),
185-
MaxLength = -1,
186-
ColumnName = "MessageTemplate",
187-
188-
};
189-
eventsTable.Columns.Add(messageTemplate);
190-
}
191-
192-
if (_columnOptions.Store.Contains(StandardColumn.Level))
193-
{
194-
var level = new DataColumn
195-
{
196-
ColumnName = "Level"
197-
};
198-
199-
if (_columnOptions.Level.StoreAsEnum)
200-
{
201-
level.DataType = typeof (byte);
202-
}
203-
else
204-
{
205-
level.DataType = typeof (string);
206-
level.MaxLength = 128;
173+
case StandardColumn.Level:
174+
eventsTable.Columns.Add(new DataColumn
175+
{
176+
DataType = _columnOptions.Level.StoreAsEnum ? typeof(byte) : typeof(string),
177+
MaxLength = _columnOptions.Level.StoreAsEnum ? 0 : 128,
178+
ColumnName = _columnOptions.Level.ColumnName ?? StandardColumn.Level.ToString()
179+
});
180+
break;
181+
case StandardColumn.TimeStamp:
182+
eventsTable.Columns.Add(new DataColumn
183+
{
184+
DataType = typeof(DateTime),
185+
ColumnName = _columnOptions.TimeStamp.ColumnName ?? StandardColumn.TimeStamp.ToString(),
186+
AllowDBNull = false
187+
});
188+
break;
189+
case StandardColumn.LogEvent:
190+
eventsTable.Columns.Add(new DataColumn
191+
{
192+
DataType = typeof(string),
193+
ColumnName = _columnOptions.LogEvent.ColumnName ?? StandardColumn.LogEvent.ToString()
194+
});
195+
break;
196+
case StandardColumn.Message:
197+
eventsTable.Columns.Add(new DataColumn
198+
{
199+
DataType = typeof(string),
200+
MaxLength = -1,
201+
ColumnName = _columnOptions.Message.ColumnName ?? StandardColumn.Message.ToString()
202+
});
203+
break;
204+
case StandardColumn.MessageTemplate:
205+
eventsTable.Columns.Add(new DataColumn
206+
{
207+
DataType = typeof(string),
208+
MaxLength = -1,
209+
ColumnName = _columnOptions.MessageTemplate.ColumnName ?? StandardColumn.MessageTemplate.ToString()
210+
});
211+
break;
212+
case StandardColumn.Exception:
213+
eventsTable.Columns.Add(new DataColumn
214+
{
215+
DataType = typeof(string),
216+
MaxLength = -1,
217+
ColumnName = _columnOptions.Exception.ColumnName ?? StandardColumn.Exception.ToString()
218+
});
219+
break;
220+
case StandardColumn.Properties:
221+
eventsTable.Columns.Add(new DataColumn
222+
{
223+
DataType = typeof(string),
224+
MaxLength = -1,
225+
ColumnName = _columnOptions.Properties.ColumnName ?? StandardColumn.Properties.ToString()
226+
});
227+
break;
207228
}
208-
209-
eventsTable.Columns.Add(level);
210-
}
211-
212-
if (_columnOptions.Store.Contains(StandardColumn.TimeStamp))
213-
{
214-
var timestamp = new DataColumn
215-
{
216-
DataType = Type.GetType("System.DateTime"),
217-
ColumnName = "TimeStamp",
218-
AllowDBNull = false
219-
};
220-
eventsTable.Columns.Add(timestamp);
221-
}
222-
223-
if (_columnOptions.Store.Contains(StandardColumn.Exception))
224-
{
225-
var exception = new DataColumn
226-
{
227-
DataType = typeof (string),
228-
MaxLength = -1,
229-
ColumnName = "Exception"
230-
};
231-
eventsTable.Columns.Add(exception);
232-
}
233-
234-
if (_columnOptions.Store.Contains(StandardColumn.Properties))
235-
{
236-
var props = new DataColumn
237-
{
238-
DataType = typeof (string),
239-
MaxLength = -1,
240-
ColumnName = "Properties",
241-
};
242-
eventsTable.Columns.Add(props);
243-
}
244-
245-
if (_columnOptions.Store.Contains(StandardColumn.LogEvent))
246-
{
247-
var eventData = new DataColumn
248-
{
249-
DataType = Type.GetType("System.String"),
250-
ColumnName = "LogEvent"
251-
};
252-
eventsTable.Columns.Add(eventData);
253229
}
254230

255231
if (_columnOptions.AdditionalDataColumns != null)
@@ -286,9 +262,7 @@ void FillDataTable(IEnumerable<LogEvent> events)
286262
row["Level"] = logEvent.Level;
287263
break;
288264
case StandardColumn.TimeStamp:
289-
row["TimeStamp"] = _columnOptions.TimeStamp.ConvertToUtc
290-
? logEvent.Timestamp.DateTime.ToUniversalTime()
291-
: logEvent.Timestamp.DateTime;
265+
row["TimeStamp"] = _columnOptions.TimeStamp.ConvertToUtc ? logEvent.Timestamp.DateTime.ToUniversalTime() : logEvent.Timestamp.DateTime;
292266
break;
293267
case StandardColumn.Exception:
294268
row["Exception"] = logEvent.Exception != null ? logEvent.Exception.ToString() : null;
@@ -336,15 +310,11 @@ private string ConvertPropertiesToXmlStructure(IEnumerable<KeyValuePair<string,
336310

337311
if (options.UsePropertyKeyAsElementName)
338312
{
339-
sb.AppendFormat("<{0}>{1}</{0}>", XmlPropertyFormatter.GetValidElementName(property.Key),
340-
value);
313+
sb.AppendFormat("<{0}>{1}</{0}>", XmlPropertyFormatter.GetValidElementName(property.Key), value);
341314
}
342315
else
343316
{
344-
sb.AppendFormat("<{0} key='{1}'>{2}</{0}>",
345-
options.PropertyElementName,
346-
property.Key,
347-
value);
317+
sb.AppendFormat("<{0} key='{1}'>{2}</{0}>", options.PropertyElementName, property.Key, value);
348318
}
349319
}
350320

@@ -388,15 +358,15 @@ private void ConvertPropertiesToColumn(DataRow row, IReadOnlyDictionary<string,
388358
if (scalarValue == null)
389359
{
390360
row[columnName] = property.Value.ToString();
391-
continue;
361+
continue;
392362
}
393363

394364
if (scalarValue.Value == null && row.Table.Columns[columnName].AllowDBNull)
395365
{
396366
row[columnName] = DBNull.Value;
397367
continue;
398368
}
399-
369+
400370
if (TryChangeType(scalarValue.Value, columnType, out conversion))
401371
{
402372
row[columnName] = conversion;

0 commit comments

Comments
 (0)