You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At minimum a connection string and table name are required.
13
+
14
+
To use a connection string from the `<connectionStrings>` element of your application config file, specify its name as the value of the connection string.
15
+
16
+
#### Code
17
+
10
18
```csharp
19
+
varconnectionString=@"Server=..."; // or the name of a connection string in your .config file
You'll need to create a table like this in your database:
17
41
18
42
```
@@ -36,26 +60,46 @@ CREATE TABLE [Logs] (
36
60
) ON [PRIMARY];
37
61
```
38
62
39
-
If you don't plan on using one or more columns, you can specify which columns to include in the *columnOptions.Store* parameter.
63
+
**Remember to grant the necessary permissions for the sink to be able to write to the log table.**
64
+
65
+
If you don't plan on using one or more columns, you can specify which columns to include in the *columnOptions.Store* parameter (see below).
66
+
40
67
The Level column should be defined as a TinyInt if the *columnOptions.Level.StoreAsEnum* is set to true.
41
68
42
-
NOTE Make sure to set up security in such a way that the sink can write to the log table.
43
69
44
-
### XML configuration
70
+
### Automatic table creation
45
71
46
-
If you are configuring Serilog with the `ReadFrom.AppSettings()` XML configuration support, you can use:
72
+
If you set the `autoCreateSqlTable` option to `true`, the sink will create a table for you in the database specified in the connection string. Make sure that the user associated with this connection string has enough rights to make schema changes.
The "standard columns" used by this sink (apart from obvious required columns like Id) are described by the StandardColumn enumeration and controlled by `columnOptions.Store`.
78
+
79
+
By default (and consistent with the SQL command to create a table, above) these columns are included:
80
+
- StandardColumn.Message
81
+
- StandardColumn.MessageTemplate
82
+
- StandardColumn.Level
83
+
- StandardColumn.TimeStamp
84
+
- StandardColumn.Exception
85
+
- StandardColumn.Properties
86
+
87
+
You can change this list, as long as the table definition is consistent:
To use a connection string from the `<connectionStrings>` element, specify its _name_ as the value of the connection string property.
97
+
You can also store your own log event properties as additional columns; see below.
98
+
55
99
56
-
### Writing properties as columns
100
+
### Saving properties in additional columns
57
101
58
-
This feature will still use all of the default columns and provide additional columns for that can be logged to (be sure to create the extra columns via SQL script first). This gives the flexibility to use as many extra columns as needed.
102
+
By default any log event properties you include in your log statements will be saved to the Properties column (and/or LogEvent column, per columnOption.Store). But they can also be stored in their own columns via the AdditionalDataColumns setting.
59
103
60
104
```csharp
61
105
varcolumnOptions=newColumnOptions
@@ -71,9 +115,20 @@ var log = new LoggerConfiguration()
The log event properties `User` and `Other` will now be placed in the corresponding column upon logging. The property name must match a column name in your table.
75
118
76
-
In addition, columns can be defined with the name and data type of the column in SQL Server. Columns specified must match database table exactly. DataType is case sensitive, based on SQL type (excluding precision/length).
119
+
The log event properties `User` and `Other` will now be placed in the corresponding column upon logging. The property name must match a column name in your table. Be sure to include them in the table definition.
120
+
121
+
122
+
#### Excluding redundant items from the Properties column
123
+
124
+
By default, additional properties will still be included in the XML data saved to the Properties column (assuming that is not disabled via the `columnOptions.Store` parameter). This is consistent with the idea behind structured logging, and makes it easier to convert the log data to another (e.g. NoSQL) storage platform later if desired.
125
+
126
+
However, if necessary, then the properties being saved in their own columns can be excluded from the XML. Use the `columnOptions.Properties.ExcludeAdditionalProperties` parameter in the sink configuration to exclude the redundant properties from the XML.
127
+
128
+
129
+
### XML configuration for columns
130
+
131
+
Columns can be defined with the name and data type of the column in SQL Server. Columns specified must match database table exactly. DataType is case sensitive, based on SQL type (excluding precision/length).
77
132
78
133
```xml
79
134
<configSections>
@@ -88,23 +143,17 @@ In addition, columns can be defined with the name and data type of the column in
88
143
</MSSqlServerSettingsSection>
89
144
```
90
145
91
-
### Auto-create Table
92
-
93
-
If you set the `autoCreateSqlTable` option to `true`, it will create a table for you in the database specified in the connection string. Make sure that the user associated with this connection string has enough rights to make schema changes.
94
-
95
-
#### Excluding redundant items from the Properties column
96
-
97
-
By default the additional properties will still be included in the XML data saved to the Properties column (assuming that is not disabled via the `columnOptions.Store` parameter). That's consistent with the idea behind structured logging, and makes it easier to convert the log data to another (e.g. NoSQL) storage platform later if desired.
146
+
### Options for serialization of the log event data
98
147
99
-
However, if the data is to stay in SQL Server, then the additional properties may not need to be saved in both columns and XML. Use the `columnOptions.Properties.ExcludeAdditionalProperties` parameter in the sink configuration to exclude the redundant properties from the XML.
148
+
#### JSON (LogEvent column)
100
149
101
-
### Saving the Log Event Data
150
+
The log event JSON can be stored to the LogEvent column. This can be enabled by adding the LogEvent column to the `columnOptions.Store` collection.
102
151
103
-
The log event JSON can be stored to the LogEvent column. This can be enabled with the `columnOptions.Store` parameter.
152
+
#### XML (Properties column)
104
153
105
-
### Options for serialization of the Properties column
154
+
To take advantage of SQL Server's XML support, the default storage of the log event properties is in the Properties XML column.
106
155
107
-
The serialization of the properties column can be controlled by setting values in the in the `columnOptions.Properties` parameter.
156
+
The serialization of the properties can be controlled by setting values in the in the `columnOptions.Properties` parameter.
108
157
109
158
Names of elements can be controlled by the `RootElementName`, `PropertyElementName`, `ItemElementName`, `DictionaryElementName`, `SequenceElementName`, `StructureElementName` and `UsePropertyKeyAsElementName` options.
110
159
@@ -114,7 +163,7 @@ If `OmitDictionaryContainerElement`, `OmitSequenceContainerElement` or `OmitStru
114
163
115
164
If `OmitElementIfEmpty` is set then if a property is empty, it will not be serialized.
116
165
117
-
### Querying the Log Property XML
166
+
#####Querying the properties XML
118
167
119
168
Extracting and querying the properties data directly can be helpful when looking for specific log sequences.
0 commit comments