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
A Serilog sink that writes events to Microsoft SQL Server. While a NoSql store allows for more flexibility to store the different kinds of properties, it sometimes is easier to use an already existing MS SQL server. This sink will write the logevent data to a table and can optionally also store the properties inside an Xml column so they can be queried.
From version 5.2 and up, this sink also support the Audit capabilities.
6
+
| **Platforms** - .NET Framework 4.5 and .NET Standard 2.0
9
7
10
8
## Configuration
11
9
12
-
At minimum a connection string and table name are required.
10
+
At minimum a connection string and table name are required.
11
+
12
+
To use a connection string from the `connectionStrings` section of your application config, specify its name as the value of the connection string.
13
+
13
14
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
+
#### Code (.NET Framework)
15
16
16
-
#### Code
17
+
Older .NET Framework applications can use the `ConfigurationManager` API shown below. Newer .NET Framework applications (using a Framework version that is .NET Standard compliant) should use the _Microsoft.Extensions.Configuration_ version in the next section.
17
18
18
19
```csharp
19
-
varconnectionString=@"Server=..."; // or the name of a connection string in your .config file
20
+
varconnectionString=@"Server=..."; // or the name of a connection string in the app config
20
21
vartableName="Logs";
21
22
varcolumnOptions=newColumnOptions(); // optional
22
23
@@ -25,9 +26,30 @@ var log = new LoggerConfiguration()
25
26
.CreateLogger();
26
27
```
27
28
28
-
#### XML
29
29
30
-
If you are configuring Serilog with the `ReadFrom.AppSettings()` XML configuration support, you can use:
30
+
#### Code (.NET Standard / .NET Core)
31
+
32
+
The application configuration parameter is optional for .NET Standard libraries or .NET Core applications.
33
+
34
+
```csharp
35
+
varappSettings=newConfigurationBuilder()
36
+
.SetBasePath(Directory.GetCurrentDirectory())
37
+
.AddJsonFile("appsettings.json")
38
+
.Build(); // more likely you will inject an IConfiguration reference
39
+
40
+
varconnectionString=@"Server=..."; // or the name of a connection string in the app config
.NET Framework libraries or applications can call `ReadFrom.AppSettings()` to configure Serilog using the [_Serilog.Settings.AppSettings_](https://github.com/serilog/serilog-settings-appsettings) package. This will apply configuration parameters from the `app.config` or `web.config` file:
#### Serilog Configuration package (.NET Standard / .NET Core)
63
+
64
+
.NET Standard libraries and .NET Core applications can call `ReadFrom.Configuration(IConfiguration)` to configure Serilog using the [_Serilog.Settings.Configuration_](https://github.com/serilog/serilog-settings-configuration) package (version [**3.0.0-dev-00111**](https://www.nuget.org/packages/Serilog.Settings.Configuration/3.0.0-dev-00111) or newer). This will apply configuration parameters from the application configuration (not only `appsettings.json` as shown here, but any other valid `IConfiguration` source):
65
+
66
+
67
+
```json
68
+
{
69
+
"Serilog": {
70
+
"Using": ["Serilog.Sinks.MSSqlServer"],
71
+
"MinimumLevel": "Debug",
72
+
"WriteTo": [
73
+
{ "Name": "MSSqlServer",
74
+
"Args": {
75
+
"connectionString": "Server...",
76
+
"tableName": "Logs"
77
+
}
78
+
}
79
+
]
80
+
}
81
+
}
82
+
```
83
+
84
+
39
85
## Table definition
40
86
41
87
You'll need to create a table like this in your database:
@@ -75,15 +121,15 @@ If you set the `autoCreateSqlTable` option to `true`, the sink will create a tab
75
121
76
122
## Standard columns
77
123
78
-
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`.
124
+
The "standard columns" used by this sink (apart from obvious required columns like Id) are described by the StandardColumn enumeration and controlled through code by the `columnOptions.Store` collection.
79
125
80
126
By default (and consistent with the SQL command to create a table, above) these columns are included:
81
-
- StandardColumn.Message
82
-
- StandardColumn.MessageTemplate
83
-
- StandardColumn.Level
84
-
- StandardColumn.TimeStamp
85
-
- StandardColumn.Exception
86
-
- StandardColumn.Properties
127
+
-`StandardColumn.Message`
128
+
-`StandardColumn.MessageTemplate`
129
+
-`StandardColumn.Level`
130
+
-`StandardColumn.TimeStamp`
131
+
-`StandardColumn.Exception`
132
+
-`StandardColumn.Properties`
87
133
88
134
You can change this list, as long as the table definition is consistent:
89
135
@@ -122,14 +168,14 @@ The log event properties `User` and `Other` will now be placed in the correspond
122
168
123
169
#### Excluding redundant items from the Properties column
124
170
125
-
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.
171
+
By default, additional properties will still be included in the data saved to the XML Properties or JSON LogEvent column (assuming one or both are enabled 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.
126
172
127
-
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.
173
+
However, if necessary, then the properties being saved in their own columns can be excluded from the data. Use the `columnOptions.Properties.ExcludeAdditionalProperties` parameter in the sink configuration to exclude the redundant properties from the XML.
128
174
129
175
130
-
### XML configuration for columns
176
+
### Columns defined by AppSettings (.NET Framework)
131
177
132
-
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).
178
+
Custom 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). This section will be processed automatically if it exists in the application's `web.config` or `app.config` file.
133
179
134
180
```xml
135
181
<configSections>
@@ -144,6 +190,72 @@ Columns can be defined with the name and data type of the column in SQL Server.
144
190
</MSSqlServerSettingsSection>
145
191
```
146
192
193
+
### ColumnOptions defined by Configuration (.NET Standard / .NET Core)
194
+
195
+
For projects using the Serilog Configuration package, most properties of the `ColumnOptions` object are configurable. (The only property not currently supported is the filter-predicate `columnOptions.Properties.PropertyFilter`).
196
+
197
+
The equivalent of adding custom columns as shown in the .NET Framework example above looks like this:
As the name suggests, `columnOptionSection` is an entire configuration section in its own right. All possible entries and some sample values are shown below. All properties and subsections are optional.
0 commit comments