Skip to content

Commit 0202c95

Browse files
authored
Merge pull request #497 from ckadluba/bug/488-traceid-aspnetcore-5
Added OpenTelemetry TraceId and SpanId
2 parents 357f24d + 1f77aa9 commit 0202c95

24 files changed

+612
-13
lines changed

.github/workflows/pr-validation.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
pull_request:
55
branches: [ dev, main ]
66

7+
# Run every biweekly to discover failures due to environment changes
8+
schedule:
9+
- cron: '0 0 1,15 * *'
10+
711
# Allows you to run this workflow manually from the Actions tab
812
workflow_dispatch:
913

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
<PackageVersion Include="Moq" Version="4.18.2" />
2424
<PackageVersion Include="xunit" Version="2.4.2" />
2525
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
26-
<PackageVersion Include="Serilog" Version="2.12.0" />
26+
<PackageVersion Include="Serilog" Version="3.1.1" />
2727
<PackageVersion Include="Serilog.Extensions.Hosting" Version="5.0.1" />
2828
<PackageVersion Include="Serilog.Settings.Configuration" Version="3.4.0" />
2929
<PackageVersion Include="Serilog.Sinks.PeriodicBatching" Version="3.1.0" />
3030
</ItemGroup>
31-
</Project>
31+
</Project>

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,22 @@ By default (and consistent with the SQL DDL to create a table shown earlier) the
406406
- `StandardColumn.Exception`
407407
- `StandardColumn.Properties`
408408

409-
There is one additional Standard Column which is not included by default (for backwards-compatibility reasons):
409+
There are the following additional standard columns which are not included by default (for backwards-compatibility reasons):
410410

411411
- `StandardColumn.LogEvent`
412+
- `StandardColumn.TraceId`
413+
- `StandardColumn.SpanId`
412414

413415
You can change this list as long as the underlying table definition is consistent:
414416

415417
```csharp
416418
// we don't need XML data
417419
columnOptions.Store.Remove(StandardColumn.Properties);
418420

419-
// we do want JSON data
421+
// we do want JSON data and OpenTelemetry
420422
columnOptions.Store.Add(StandardColumn.LogEvent);
423+
columnOptions.Store.Add(StandardColumn.TraceId);
424+
columnOptions.Store.Add(StandardColumn.SpanId);
421425
```
422426

423427
In addition to any special properties described below, each Standard Column also has the usual column properties like `ColumnName` as described in the topic [SqlColumn Objects](#sqlcolumn-objects).
@@ -454,7 +458,7 @@ In case `DataLength` is set to a specific value different from -1, any message l
454458

455459
This column stores the log event message with the property placeholders. It defaults to `nvarchar(max)`. The `DataType` property can only be set to character-storage types.
456460

457-
In case `DataLength` is set to a specific value different from -1, any template text longer than that length will be effectively truncated to that size. Any truncating ignores all differences between the tokens in the template meaning that a template might get cut off in the middle of a property token. Example: `DataLength` is set to 20 and the message template is "a long {NumberOfCharacters} template text" (without the quotes), the final template stored in the database will be: "a long {NumberOfC..." (again without quotes).
461+
If `DataLength` is set to a value different to -1 longer text will be truncated. See [Message column](#message) for details.
458462

459463
### Level
460464

@@ -484,7 +488,7 @@ When the `ConvertToUtc` property is set to `true`, the time stamp is adjusted to
484488

485489
When an exception is logged as part of the log event, the exception message is stored here automatically. The `DataType` must be `nvarchar`.
486490

487-
Similar to the columns `Message` and `MessageTemplate`, setting `DataLength` of `Exception` to a specific value different from -1 will effectively truncate any exception message to the stated length in `DataLength`.
491+
Similar to the columns `Message` and `MessageTemplate`, setting `DataLength` to a specific value different from -1 will effectively truncate any exception message to the stated length in `DataLength`. See [Message column](#message ) for details.
488492

489493
### Properties
490494

@@ -506,13 +510,22 @@ If `OmitElementIfEmpty` is set then if a property is empty, it will not be seria
506510

507511
This column stores log event property values as JSON. Typically you will use either this column or the XML-based `Properties` column, but not both. This column's `DataType` must always be `nvarchar`.
508512

509-
The `ExcludeAddtionalProperties` and `ExcludeStandardColumns` properties are described in the [Custom Property Columns](#custom-property-columns) topic.
513+
By default this column is not used unless it is added to the `ColumnOptions.Store` property as documented [above](#standard-columns).
510514

511515
The content of this column is rendered as JSON by default or with a custom ITextFormatter passed by the caller as parameter `logEventFormatter`. Details can be found in [Sink Configuration](#sink-configuration).
512516

517+
### TraceId and SpanId
518+
519+
These two columns store the OpenTelemetry `TraceId` and `SpanId` log event properties which are documented [here](https://github.com/serilog/serilog/issues/1923). The `DataType` of these columns must be `nvarchar` or `varchar`.
520+
521+
By default these columns are not used unless they are added to the `ColumnOptions.Store` property as documented [above](#standard-columns).
522+
513523
## Custom Property Columns
514524

515-
By default, any log event properties you include in your log statements will be saved to the XML `Properties` column or the JSON `LogEvent` column. But they can also be stored in their own individual columns via the `AdditionalColumns` collection. This adds overhead to write operations but is very useful for frequently-queried properties. Only `ColumnName` is required; the default configuration is `varchar(max)`. If you specify a DataLength on a column of character data types (NVarChar, VarChar, Char, NChar) the string will be automatically truncated to the datalength to fit in the column.
525+
By default, any log event properties you include in your log statements will be saved to the XML `Properties` column or the JSON `LogEvent` column. But they can also be stored in their own individual columns via the `AdditionalColumns` collection. This adds overhead to write operations but is very useful for frequently-queried properties. Only `ColumnName` is required; the default configuration is `varchar(max)`.
526+
527+
If you specify a DataLength other than -1 on a column of character data types (NVarChar, VarChar, Char, NChar) longer text will be truncated to the specified length. See [Message column](#message ) for details.
528+
516529

517530
```csharp
518531
var columnOptions = new ColumnOptions
@@ -601,7 +614,7 @@ As the name suggests, `columnOptionSection` is an entire configuration section i
601614
"disableTriggers": true,
602615
"clusteredColumnstoreIndex": false,
603616
"primaryKeyColumnName": "Id",
604-
"addStandardColumns": [ "LogEvent" ],
617+
"addStandardColumns": [ "LogEvent", "TraceId", "SpanId" ],
605618
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
606619
"additionalColumns": [
607620
{ "ColumnName": "EventType", "DataType": "int", "AllowNull": false },
@@ -665,6 +678,8 @@ Keys and values are case-sensitive. Case must match **_exactly_** as shown below
665678
<!-- ColumnOptions parameters -->
666679
<AddStandardColumns>
667680
<add Name="LogEvent"/>
681+
<add Name="TraceId"/>
682+
<add Name="SpanId"/>
668683
</AddStandardColumns>
669684
<RemoveStandardColumns>
670685
<remove Name="Properties"/>

sample/WorkerServiceDemo/appsettings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"restrictedToMinimumLevel": "Information",
2323
"columnOptionsSection": {
24-
"addStandardColumns": [ "LogEvent" ],
24+
"addStandardColumns": [ "LogEvent", "TraceId", "SpanId" ],
2525
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
2626
"timeStamp": {
2727
"columnName": "Timestamp",
@@ -54,10 +54,10 @@
5454
"sinkOptionsSection": {
5555
"tableName": "LogEventsAudit",
5656
"autoCreateSqlDatabase": true,
57-
"autoCreateSqlTable": true,
57+
"autoCreateSqlTable": true
5858
},
5959
"columnOptionsSection": {
60-
"addStandardColumns": [ "LogEvent" ],
60+
"addStandardColumns": [ "LogEvent", "TraceId", "SpanId" ],
6161
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
6262
"timeStamp": {
6363
"columnName": "Timestamp",

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/Microsoft.Extensions.Configuration/MicrosoftExtensionsColumnOptionsProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ private static void ReadStandardColumns(IConfigurationSection config, ColumnOpti
147147
section = config.GetSection("messageTemplate");
148148
if (section != null)
149149
SetCommonColumnOptions(section, columnOptions.MessageTemplate);
150+
151+
section = config.GetSection("traceId");
152+
if (section != null)
153+
{
154+
SetCommonColumnOptions(section, columnOptions.TraceId);
155+
}
156+
157+
section = config.GetSection("spanId");
158+
if (section != null)
159+
{
160+
SetCommonColumnOptions(section, columnOptions.SpanId);
161+
}
150162
}
151163

152164
private static void ReadMiscColumnOptions(IConfigurationSection config, ColumnOptions columnOptions)

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/MSSqlServerConfigurationSection.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ public StandardColumnConfigLevel Level
106106
get => (StandardColumnConfigLevel)base[nameof(Level)];
107107
}
108108

109+
[ConfigurationProperty(nameof(TraceId))]
110+
public StandardColumnConfigTraceId TraceId
111+
{
112+
get => (StandardColumnConfigTraceId)base[nameof(TraceId)];
113+
}
114+
115+
[ConfigurationProperty(nameof(SpanId))]
116+
public StandardColumnConfigSpanId SpanId
117+
{
118+
get => (StandardColumnConfigSpanId)base[nameof(SpanId)];
119+
}
120+
109121
[ConfigurationProperty(nameof(LogEvent))]
110122
public StandardColumnConfigLogEvent LogEvent
111123
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Configuration;
2+
3+
// Disable XML comment warnings for internal config classes which are required to have public members
4+
#pragma warning disable 1591
5+
6+
namespace Serilog.Sinks.MSSqlServer
7+
{
8+
public class StandardColumnConfigSpanId : ColumnConfig
9+
{
10+
public StandardColumnConfigSpanId() : base()
11+
{ }
12+
13+
// override to set IsRequired = false
14+
[ConfigurationProperty("ColumnName", IsRequired = false, IsKey = true)]
15+
public override string ColumnName
16+
{
17+
get => base.ColumnName;
18+
set => base.ColumnName = value;
19+
}
20+
}
21+
}
22+
23+
#pragma warning restore 1591
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Configuration;
2+
3+
// Disable XML comment warnings for internal config classes which are required to have public members
4+
#pragma warning disable 1591
5+
6+
namespace Serilog.Sinks.MSSqlServer
7+
{
8+
public class StandardColumnConfigTraceId : ColumnConfig
9+
{
10+
public StandardColumnConfigTraceId() : base()
11+
{ }
12+
13+
// override to set IsRequired = false
14+
[ConfigurationProperty("ColumnName", IsRequired = false, IsKey = true)]
15+
public override string ColumnName
16+
{
17+
get => base.ColumnName;
18+
set => base.ColumnName = value;
19+
}
20+
}
21+
}
22+
23+
#pragma warning restore 1591
24+

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/SystemConfigurationColumnOptionsProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ private static void ReadStandardColumns(MSSqlServerConfigurationSection config,
105105
SetCommonColumnOptions(config.Id, columnOptions.Id);
106106
SetCommonColumnOptions(config.Level, columnOptions.Level);
107107
SetCommonColumnOptions(config.LogEvent, columnOptions.LogEvent);
108+
SetCommonColumnOptions(config.TraceId, columnOptions.TraceId);
109+
SetCommonColumnOptions(config.SpanId, columnOptions.SpanId);
108110
SetCommonColumnOptions(config.Message, columnOptions.Message);
109111
SetCommonColumnOptions(config.MessageTemplate, columnOptions.MessageTemplate);
110112
SetCommonColumnOptions(config.PropertiesColumn, columnOptions.Properties);

src/Serilog.Sinks.MSSqlServer/Serilog.Sinks.MSSqlServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>A Serilog sink that writes events to Microsoft SQL Server</Description>
5-
<VersionPrefix>6.4.0</VersionPrefix>
5+
<VersionPrefix>6.5.0</VersionPrefix>
66
<Authors>Michiel van Oudheusden;Christian Kadluba;Serilog Contributors</Authors>
77
<TargetFrameworks>netstandard2.0;net462;net472;net6.0</TargetFrameworks>
88
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

0 commit comments

Comments
 (0)