|
| 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 | + |
0 commit comments