-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* NBB.MultiTenancy.Serilog * tests and cleanup * documentation update Co-authored-by: [email protected] <[email protected]>
- Loading branch information
1 parent
5000bd2
commit 9528ffe
Showing
14 changed files
with
542 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
....Serilog.Enrichers.ServiceIdentifier/NBB.Tools.Serilog.Enrichers.ServiceIdentifier.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog" Version="$(SerilogPackageVersion)" /> | ||
</ItemGroup> | ||
</Project> |
71 changes: 71 additions & 0 deletions
71
src/Tools/Serilog/NBB.Tools.Serilog.Enrichers.ServiceIdentifier/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# NBB.Tools.Serilog.Enrichers.ServiceIdentifier | ||
|
||
This project provides a Serilog Enricher that adds a service identifier to the log context. | ||
The identifier is taken either from messaging source or from the Assembly.GetEntryAssembly().Name. | ||
|
||
|
||
## NuGet install | ||
``` | ||
dotnet add package NBB.Tools.Serilog.Enrichers.ServiceIdentifier | ||
``` | ||
# Registration | ||
The enricher should be registered in `Startup.cs`: | ||
|
||
```csharp | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
... | ||
services.AddSingleton<ServiceIdentifierEnricher>(); | ||
... | ||
} | ||
``` | ||
# Usage: example for Program.cs | ||
```csharp | ||
var hostBuilder = CreateHostBuilder(args); | ||
var tempLogger = new LoggerConfiguration() | ||
.ReadFrom.Configuration(Configuration) | ||
.CreateLogger(); | ||
|
||
hostBuilder.UseSerilog((context, services, configuration) => | ||
{ | ||
configuration.ReadFrom.Configuration(Configuration); | ||
configuration.Enrich.With(services.GetRequiredService<ServiceIdentifierEnricher>()); | ||
... | ||
}); | ||
``` | ||
|
||
# Usage: example for an sql logger configured in appsettings.json | ||
"Serilog": { | ||
"MinimumLevel": "Debug", | ||
"tableName": "TABLE_NAME", | ||
"WriteTo": [ | ||
{ | ||
"Name": "MSSqlServer", | ||
"Args": { | ||
"connectionString": "Server=SERVER,POST;Database=DB;User Id=USERID;Password=PASS;MultipleActiveResultSets=true", | ||
"tableName": "TABLE_NAME", | ||
"autoCreateSqlTable": false, | ||
"columnOptionsSection": { | ||
"addStandardColumns": [ "LogEvent" ], | ||
"customColumns": [ | ||
{ | ||
"ColumnName": "ServiceIdentifier", | ||
"PropertyName": "ServiceIdentifier", | ||
"DataType": "nvarchar", | ||
"DataLength": "255", | ||
"AllowNull": true | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"Name": "Console", | ||
"Args": { | ||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} T: {TenantId} E: {EnvelopeGuid} N: {FirstName} {LastName} ID: {IdCardIdentifier} EID: {FingerprintId} {NewLine}{Exception}" | ||
} | ||
} | ||
], | ||
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "ServiceIdentifier" ] | ||
}, | ||
|
31 changes: 31 additions & 0 deletions
31
src/Tools/Serilog/NBB.Tools.Serilog.Enrichers.ServiceIdentifier/ServiceIdentifierEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using System.Reflection; | ||
|
||
namespace NBB.Tools.Serilog.Enrichers.ServiceIdentifier | ||
{ | ||
public class ServiceIdentifierEnricher : ILogEventEnricher | ||
{ | ||
private readonly IConfiguration _configuration; | ||
public static string PropertyName { get; } = "ServiceIdentifier"; | ||
|
||
public ServiceIdentifierEnricher(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
var source = _configuration.GetSection("Messaging")?["Source"]; | ||
if (string.IsNullOrEmpty(source)) | ||
{ | ||
source = Assembly.GetEntryAssembly().GetName().Name; | ||
} | ||
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty(PropertyName, source)); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../Serilog/NBB.Tools.Serilog.Enrichers.TenantId/NBB.Tools.Serilog.Enrichers.TenantId.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog" Version="$(SerilogPackageVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\MultiTenancy\NBB.MultiTenancy.Abstractions\NBB.MultiTenancy.Abstractions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
76 changes: 76 additions & 0 deletions
76
src/Tools/Serilog/NBB.Tools.Serilog.Enrichers.TenantId/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# NBB.Tools.Serilog.Enrichers.TenantId | ||
|
||
This project provides a Serilog Enricher that adds the tenant id to the log context. | ||
It can be the case that logging is requested before tenant identification is requested or that tenant cannot be found. In this case, the empty guid will be set in the context. | ||
|
||
|
||
## NuGet install | ||
``` | ||
dotnet add package NBB.MultiTenancy.Serilog | ||
``` | ||
# Registration | ||
The enricher should be registered in `Startup.cs`: | ||
|
||
```csharp | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
... | ||
services.AddSingleton<TenantEnricher>(); | ||
... | ||
} | ||
``` | ||
# Usage: example for Program.cs | ||
```csharp | ||
var hostBuilder = CreateHostBuilder(args); | ||
var tempLogger = new LoggerConfiguration() | ||
.ReadFrom.Configuration(Configuration) | ||
.CreateLogger(); | ||
|
||
hostBuilder.UseSerilog((context, services, configuration) => | ||
{ | ||
configuration.ReadFrom.Configuration(Configuration); | ||
configuration.Enrich.With(services.GetRequiredService<TenantEnricher>()); | ||
... | ||
}); | ||
``` | ||
|
||
# Usage: example for an sql logger configured in appsettings.json | ||
"Serilog": { | ||
"MinimumLevel": "Debug", | ||
"tableName": "TABLE_NAME", | ||
"WriteTo": [ | ||
{ | ||
"Name": "MSSqlServer", | ||
"Args": { | ||
"connectionString": "Server=SERVER,POST;Database=DB;User Id=USERID;Password=PASS;MultipleActiveResultSets=true", | ||
"tableName": "TABLE_NAME", | ||
"autoCreateSqlTable": false, | ||
"columnOptionsSection": { | ||
"addStandardColumns": [ "LogEvent" ], | ||
"customColumns": [ | ||
{ | ||
"ColumnName": "TenantId", | ||
"PropertyName": "TenantId", | ||
"DataType": "uniqueidentifier", | ||
"AllowNull": false | ||
}, | ||
{ | ||
"ColumnName": "OtherId", | ||
"PropertyName": "OtherId", | ||
"DataType": "uniqueidentifier", | ||
"AllowNull": true | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"Name": "Console", | ||
"Args": { | ||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} T: {TenantId} E: {EnvelopeGuid} N: {FirstName} {LastName} ID: {IdCardIdentifier} EID: {FingerprintId} {NewLine}{Exception}" | ||
} | ||
} | ||
], | ||
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "Tenant" ] | ||
}, | ||
|
32 changes: 32 additions & 0 deletions
32
src/Tools/Serilog/NBB.Tools.Serilog.Enrichers.TenantId/TenantEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
using NBB.MultiTenancy.Abstractions; | ||
using NBB.MultiTenancy.Abstractions.Context; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace NBB.Tools.Serilog.Enrichers.TenantId | ||
{ | ||
public class TenantEnricher : ILogEventEnricher | ||
{ | ||
private readonly ITenantContextAccessor _tenantContextAccessor; | ||
public static string PropertyName { get; } = nameof(Tenant.Default.TenantId); | ||
|
||
public TenantEnricher(ITenantContextAccessor tenantContextAccessor) | ||
{ | ||
_tenantContextAccessor = tenantContextAccessor; | ||
} | ||
|
||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
if (_tenantContextAccessor.TenantContext == null) | ||
{ | ||
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty(PropertyName, Tenant.Default.TenantId)); | ||
return; | ||
} | ||
var tenantId = _tenantContextAccessor.TenantContext.TryGetTenantId(); | ||
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty(PropertyName, tenantId ?? Tenant.Default.TenantId)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Serilog tools | ||
|
||
NBB serilog tools provide enrichers and sinks for NBB. | ||
|
||
The package [`NBB.Tools.Serilog.Enrichers.ServiceIdentifier`](NBB.Tools.Serilog.Enrichers.ServiceIdentifier) provides an enricher for service identifier. It is taken from nbb source / entry assembly name / process name. | ||
|
||
The package [`NBB.Tools.Serilog.Enrichers.TenantId`](NBB.Tools.Serilog.Enrichers.TenantId) provides an enricher for tenant id from nbb tenant context. | ||
|
||
The package [`NBB.Tools.Serilog.OpenTracingSink`](NBB.Tools.Serilog.OpenTracingSink) provides a sink for serilog and opentracing. |
27 changes: 27 additions & 0 deletions
27
...ichers.ServiceIdentifier.Tests/NBB.Tools.Serilog.Enrichers.ServiceIdentifier.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsPackageVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsPackagesVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsPackagesVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackagesVersion)" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkPackageVersion)" /> | ||
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" /> | ||
<PackageReference Include="xunit" Version="$(XunitPackageVersion)" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitRunnerVisualStudioPackageVersion)" /> | ||
<PackageReference Include="coverlet.collector" Version="$(CoverletCollectorPackageVersion)"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\Tools\Serilog\NBB.Tools.Serilog.Enrichers.ServiceIdentifier\NBB.Tools.Serilog.Enrichers.ServiceIdentifier.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.