Skip to content

Commit 5e12251

Browse files
Merge pull request #109 from Stravaig-Projects/#87-log-directly-to-serilog
#87 Log directly to Serilog
2 parents 889e7a5 + be31370 commit 5e12251

File tree

45 files changed

+2442
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2442
-90
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"Fallah",
55
"Mackay",
66
"NUGET",
7+
"Serilog",
78
"Shouldly",
89
"Stravaig",
910
"nunit",

docs/index.rst

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Stravaig Projects: Configuration Diagnostics documentation!
77
===========================================================
88

9-
``Stravaig.Extensions.Configuration.Diagnostics`` is a set
9+
``Stravaig.Configuration.Diagnostics`` is a set
1010
of extensions to assist diagnosing configuration setting
1111
issues by logging the current state which allows them to
1212
be examined. It also provides options for obfuscating
@@ -17,17 +17,32 @@ logs.
1717
:maxdepth: 2
1818
:caption: Introduction:
1919

20-
intro/quick-start
20+
intro/quick-start-logging
21+
intro/quick-start-serilog
2122
intro/installing-the-package
22-
intro/configuration-values
23-
intro/configuration-providers
24-
intro/track-value-source
25-
intro/connection-strings
2623
intro/diagnostics-options
2724

2825
.. toctree::
2926
:maxdepth: 2
30-
:caption: advanced
27+
:caption: Microsoft Logging Extensions
28+
29+
logging-extensions/configuration-values
30+
logging-extensions/configuration-providers
31+
logging-extensions/track-value-source
32+
logging-extensions/connection-strings
33+
34+
.. toctree::
35+
:maxdepth: 2
36+
:caption: Serilog Extensions
37+
38+
serilog/configuration-values
39+
serilog/configuration-providers
40+
serilog/track-value-source
41+
serilog/connection-strings
42+
43+
.. toctree::
44+
:maxdepth: 2
45+
:caption: Advanced
3146

3247
advanced/creating-obfuscators
3348
advanced/creating-matchers

docs/intro/installing-the-package.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,39 @@ The latest packages are available on `NuGet`_.
2323
* - Stravaig.Configuration.Diagnostics.Logging
2424
- .. image:: https://img.shields.io/nuget/v/Stravaig.Configuration.Diagnostics.Logging?color=004880&label=nuget%20stable&logo=nuget
2525
- .. image:: https://img.shields.io/nuget/vpre/Stravaig.Configuration.Diagnostics.Logging?color=ffffff&label=nuget%20latest&logo=nuget)
26+
* - Stravaig.Configuration.Diagnostics.Serilog
27+
- .. image:: https://img.shields.io/nuget/v/Stravaig.Configuration.Diagnostics.Serilog?color=004880&label=nuget%20stable&logo=nuget
28+
- .. image:: https://img.shields.io/nuget/vpre/Stravaig.Configuration.Diagnostics.Serilog?color=ffffff&label=nuget%20latest&logo=nuget)
2629

2730
Installing from a PowerShell prompt
2831
-----------------------------------
2932

30-
You can install the package into your project from a PowerShell prompt. Navigate to the folder your project file is in and type:
33+
You can install the package into your project from a PowerShell
34+
prompt. Navigate to the folder your project file is in and type:
3135

3236
::
3337

3438
Install-Package Stravaig.Configuration.Diagnostics.Logging
3539

40+
or
41+
42+
::
43+
44+
Install-Package Stravaig.Configuration.Diagnostics.Serilog
45+
3646
Installing using the .NET CLI
3747
-----------------------------
3848

39-
You can install the package into your project with the .NET CLI command. Navigate to the folder your project file is in and type:
49+
You can install the package into your project with the .NET CLI
50+
command. Navigate to the folder your project file is in and type:
4051

4152
::
4253

4354
dotnet add package Stravaig.Configuration.Diagnostics.Logging
4455

56+
or
57+
58+
::
59+
60+
dotnet add package Stravaig.Configuration.Diagnostics.Serilog
61+

docs/intro/quick-start.rst renamed to docs/intro/quick-start-logging.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Quick start
2-
===========
1+
Quick start with Logging Extensions
2+
===================================
33

4-
Install the ``Stravaig.Extensions.Configuration.Diagnostics`` into your main project. (See :ref:`Installing <refInstalling>` for details of installing the NuGet Package.
4+
Install the ``Stravaig.Configuration.Diagnostics.Logging`` into your main project. (See :ref:`Installing <refInstalling>` for details of installing the NuGet Package.
55

66
In your ``Startup`` class in the ``Configure`` method add the following:
77

docs/intro/quick-start-serilog.rst

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
Quick start with Serilog
2+
========================
3+
4+
Install the ``Stravaig.Configuration.Diagnostics.Serilog`` into
5+
your main project. (See :ref:`Installing <refInstalling>` for
6+
details of installing the NuGet Package.
7+
8+
In your ``Startup`` class in the ``Configure`` or
9+
``ConfigureServices`` method add the following:
10+
11+
::
12+
13+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
14+
{
15+
// Get the logger
16+
var logger = Log.ForContext<Startup>();
17+
18+
// Define which keys contain secrets
19+
ConfigurationDiagnosticsOptions.SetupBy
20+
.Obfuscating.WithAsterisks()
21+
.And.MatchingConfigurationKeys.Where
22+
.KeyContains("AccessToken")
23+
.OrContains("ConnectionString")
24+
.And.MatchingConnectionStringKeys
25+
.Containing("Password")
26+
.AndFinally.ApplyOptions(ConfigurationDiagnosticsOptions.GlobalOptions);
27+
28+
// Log the state of the configuration
29+
logger.LogProvidersAsInformation(Configuration);
30+
logger.LogConfigurationValuesAsInformation(Configuration);
31+
logger.LogConfigurationKeySourceAsInformation(Configuration, "ExtenalSystem:AccessToken");
32+
logger.LogAllConnectionStringsAsInformation(Configuration);
33+
34+
// Set up other things...
35+
}
36+
37+
The above setup up the diagnostics as follows
38+
39+
* Any secret that is found is obfuscated by replacing the value with asterisks.
40+
* Any configuration key that contains "AccessToken" is considered a secret an will be obfuscated.
41+
* Any configuration key that contains "ConnectionString" is considered a secret.
42+
* When deconstructing connection strings, any part where the key contains "password"is considered a secret.
43+
44+
LogProvidersAsInformation
45+
-------------------------
46+
47+
The output will look something like this:
48+
49+
::
50+
51+
info: Example.Startup[0]
52+
The following configuration providers were registered:
53+
Microsoft.Extensions.Configuration.ChainedConfigurationProvider
54+
JsonConfigurationProvider for 'appsettings.json' (Optional)
55+
JsonConfigurationProvider for 'appsettings.Development.json' (Optional)
56+
JsonConfigurationProvider for 'secrets.json' (Optional)
57+
EnvironmentVariablesConfigurationProvider
58+
CommandLineConfigurationProvider
59+
60+
LogConfigurationValuesAsInformation
61+
-----------------------------------
62+
63+
The output will look something like this:
64+
65+
::
66+
67+
info: Example.Startup[0]
68+
The following values are available:
69+
Logging : (null)
70+
Logging:LogLevel : (null)
71+
Logging:LogLevel:Microsoft.Hosting.Lifetime : Information
72+
Logging:LogLevel:Microsoft : Warning
73+
Logging:LogLevel:Default : Information
74+
ExternalSystem : (null)
75+
ExternalSystem:Url : https://dev.some-external-system.com/api/v1
76+
ExternalSystem:AccessToken : *****************************
77+
ExtenalSystem : (null)
78+
ExtenalSystem:AccessToken : **************************************
79+
ENVIRONMENT : Development
80+
ConnectionStrings :
81+
ConnectionStrings:PostCodeLookupDatabase : **********************************************************************************************************************************************************************************
82+
ConnectionStrings:MainDatabase : ********************************************************************************************************
83+
ComSpec : C:\WINDOWS\system32\cmd.exe
84+
ASPNETCORE_URLS : https://localhost:5001;http://localhost:5000
85+
ASPNETCORE_ENVIRONMENT : Development
86+
applicationName : Example
87+
AllowedHosts : *
88+
89+
Note: I've omitted many of the environment variables from the above example.
90+
91+
One of the things you can see here, is that there is a typo in one of the configuration providers. If we had an issue with the ``ExternalSystem.AccessToken`` we can now see that somewhere it has been mistyped.
92+
93+
LogConfigurationKeySourceAsInformation
94+
--------------------------------------
95+
96+
It is possible to trace where a specific key value came from, so in the case of the key with the typo, you can add a line to log out the source of that specific key/value.
97+
98+
::
99+
100+
logger.LogConfigurationKeySourceAsInformation(Configuration, "ExtenalSystem:AccessToken");
101+
102+
The output looks something like this:
103+
104+
::
105+
106+
info: Example.Startup[0]
107+
Provider sources for value of ExtenalSystem:AccessToken
108+
* Microsoft.Extensions.Configuration.ChainedConfigurationProvider ==> null
109+
* JsonConfigurationProvider for 'appsettings.json' (Optional) ==> null
110+
* JsonConfigurationProvider for 'appsettings.Development.json' (Optional) ==> null
111+
* JsonConfigurationProvider for 'secrets.json' (Optional) ==> **************************************
112+
* EnvironmentVariablesConfigurationProvider ==> null
113+
* CommandLineConfigurationProvider ==> null
114+
115+
As the only provider with a value for the key with the typo is the ``secrets.json`` file we can instantly tell where the issue is.
116+
117+
118+
LogAllConnectionStringsAsInformation
119+
------------------------------------
120+
121+
Although we've designated that any configuration key that matches ``ConnectionString`` has a secret value associated with it, we can deconstruct a connection string into its component parts as they are not all secrets. This way you can examine a large portion of a connection string without exposing, for example, the password used to access it.
122+
123+
The output looks something like this:
124+
125+
::
126+
127+
info: Example.Startup[0]
128+
The following connection strings were found: MainDatabase, PostCodeLookupDatabase.
129+
Connection string (named MainDatabase) parameters:
130+
* server = dev.my-database-server.my-company.com
131+
* database = myDataBase
132+
* user id = myUsername
133+
* password = **********
134+
135+
Connection string (named PostCodeLookupDatabase) parameters:
136+
* provider = MSOLEDBSQL
137+
* server = tcp:AvailabilityGroupListenerDnsName,1433
138+
* multisubnetfailover = Yes
139+
* applicationintent = ReadOnly
140+
* database = MyDB
141+
* integrated security = SSPI
142+
* connect timeout = 30
143+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)