Skip to content

Adding support for MSI (Managed Service Identities), and AccessTokens… #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ end_of_line = lf

[*.{cmd, bat}]
end_of_line = crlf

[*.cs]

# CA2227: Collection properties should be read only
dotnet_diagnostic.CA2227.severity = none

# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = none

# S108: Nested blocks of code should not be left empty
dotnet_diagnostic.S108.severity = none
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A Serilog sink that writes events to Microsoft SQL Server. This sink will write the log event data to a table and can optionally also store the properties inside an XML or JSON column so they can be queried. Important properties can also be written to their own separate columns.

**Package** - [Serilog.Sinks.MSSqlServer](http://nuget.org/packages/serilog.sinks.mssqlserver)
| **Minimum Platforms** - .NET Framework 4.5.2, .NET Core 2.0, .NET Standard 2.0
| **Minimum Platforms** - .NET Framework 4.6.1, .NET Core 2.0, .NET Standard 2.0

#### Topics

Expand Down Expand Up @@ -34,9 +34,13 @@ All sink configuration methods accept the following arguments, though not necess
* `batchPostingLimit`
* `period`
* `formatProvider`
* `useMsi`
* `azureServiceTokenProviderResource`

### Basic Arguments

Adding support for MSI (Managed Service Identities), and AccessTokens in sqlConnections.

At minimum, `connectionString` and `tableName` are required. If you are using an external configuration source such as an XML file or JSON file, you can use a named connection string instead of providing the full "raw" connection string.

If `schemaName` is omitted, the default is `dbo`.
Expand Down Expand Up @@ -70,7 +74,6 @@ Because of the way external configuration has been implemented in various .NET f

| Your Framework | TFM | Project Types | External Configuration |
| --- | --- | --- | --- |
| .NET Framework 4.5.2 | `net452` | app or library | _System.Configuration_ |
| .NET Framework 4.6.1+ | `net461` | app or library | _System.Configuration_ |
| .NET Framework 4.6.1+ | `net461` | app or library | _Microsoft.Extensions.Configuration_ |
| .NET Standard 2.0 | `netstandard2.0` | library only | _Microsoft.Extensions.Configuration_ |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static class LoggerConfigurationMSSqlServerExtensions
/// <summary>
/// The configuration section name for app.config or web.config configuration files.
/// </summary>
public static string AppConfigSectionName = "MSSqlServerSettingsSection";
public static string AppConfigSectionName { get; } = "MSSqlServerSettingsSection";


/// <summary>
/// Adds a sink that writes log events to a table in a MSSqlServer database.
Expand All @@ -54,6 +55,8 @@ public static class LoggerConfigurationMSSqlServerExtensions
/// <param name="columnOptions">An externally-modified group of column settings</param>
/// <param name="columnOptionsSection">A config section defining various column settings</param>
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
/// <param name="useMsi">Option to use MSI</param>
/// <param name="azureServiceTokenProviderResource">Resource required in AzureServiceTokenProvider.GetAccessTokenAsync(azureServiceTokenProviderResource). This will error if null, and useMsi is st to true</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration MSSqlServer(
Expand All @@ -68,20 +71,28 @@ public static LoggerConfiguration MSSqlServer(
bool autoCreateSqlTable = false,
ColumnOptions columnOptions = null,
IConfigurationSection columnOptionsSection = null,
string schemaName = "dbo"
string schemaName = "dbo",
bool useMsi = false,
string azureServiceTokenProviderResource = null
)
{
if(loggerConfiguration == null)
if (useMsi && string.IsNullOrWhiteSpace(azureServiceTokenProviderResource))
throw new ArgumentNullException(nameof(azureServiceTokenProviderResource), "If useMsi is set to true, you must also provide an azureServiceTokenProviderResource");

if (loggerConfiguration == null)
throw new ArgumentNullException("loggerConfiguration");

var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
var colOpts = columnOptions ?? new ColumnOptions();
var connStr = connectionString;
var tokenResource = azureServiceTokenProviderResource;

if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
{
colOpts = ApplySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
connStr = ApplySystemConfiguration.GetConnectionString(connStr);
if(useMsi)
tokenResource = ApplySystemConfiguration.GetAzureServiceTokenProviderResource(tokenResource);

if (appConfiguration != null || columnOptionsSection != null)
SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink.");
Expand All @@ -91,6 +102,8 @@ public static LoggerConfiguration MSSqlServer(
{
connStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
if (useMsi)
tokenResource = ApplyMicrosoftExtensionsConfiguration.GetAzureServiceTokenProviderResource(tokenResource, appConfiguration);
}

return loggerConfiguration.Sink(
Expand All @@ -102,7 +115,9 @@ public static LoggerConfiguration MSSqlServer(
formatProvider,
autoCreateSqlTable,
colOpts,
schemaName
schemaName,
useMsi,
tokenResource
),
restrictedToMinimumLevel);
}
Expand All @@ -120,6 +135,8 @@ public static LoggerConfiguration MSSqlServer(
/// <param name="columnOptions">An externally-modified group of column settings</param>
/// <param name="columnOptionsSection">A config section defining various column settings</param>
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
/// <param name="useMsi">Option to use MSI</param>
/// <param name="azureServiceTokenProviderResource">Resource required in AzureServiceTokenProvider.GetAccessTokenAsync(azureServiceTokenProviderResource). This will error if null, and useMsi is st to true</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration MSSqlServer(
Expand All @@ -132,19 +149,26 @@ public static LoggerConfiguration MSSqlServer(
bool autoCreateSqlTable = false,
ColumnOptions columnOptions = null,
IConfigurationSection columnOptionsSection = null,
string schemaName = "dbo"
string schemaName = "dbo",
bool useMsi = false,
string azureServiceTokenProviderResource = null
)
{
if(loggerAuditSinkConfiguration == null)
if (useMsi && string.IsNullOrWhiteSpace(azureServiceTokenProviderResource))
throw new ArgumentNullException(nameof(azureServiceTokenProviderResource), "If useMsi is set to true, you must also provide an azureServiceTokenProviderResource");

if (loggerAuditSinkConfiguration == null)
throw new ArgumentNullException("loggerAuditSinkConfiguration");

var colOpts = columnOptions ?? new ColumnOptions();
var connStr = connectionString;
var tokenResource = azureServiceTokenProviderResource;

if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
{
colOpts = ApplySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
connStr = ApplySystemConfiguration.GetConnectionString(connStr);
tokenResource = ApplySystemConfiguration.GetAzureServiceTokenProviderResource(tokenResource);

if (appConfiguration != null || columnOptionsSection != null)
SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink.");
Expand All @@ -154,6 +178,7 @@ public static LoggerConfiguration MSSqlServer(
{
connStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
tokenResource = ApplyMicrosoftExtensionsConfiguration.GetAzureServiceTokenProviderResource(tokenResource, appConfiguration);
}

return loggerAuditSinkConfiguration.Sink(
Expand All @@ -163,7 +188,9 @@ public static LoggerConfiguration MSSqlServer(
formatProvider,
autoCreateSqlTable,
colOpts,
schemaName
schemaName,
useMsi,
tokenResource
),
restrictedToMinimumLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static partial class LoggerConfigurationMSSqlServerExtensions
/// <param name="columnOptions">An externally-modified group of column settings</param>
/// <param name="columnOptionsSection">A config section defining various column settings</param>
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
/// <param name="useMsi">Option to use MSI</param>
/// <param name="azureServiceTokenProviderResource">Resource required in AzureServiceTokenProvider.GetAccessTokenAsync(azureServiceTokenProviderResource). This will error if null, and useMsi is st to true</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration MSSqlServer(
Expand All @@ -59,15 +61,21 @@ public static LoggerConfiguration MSSqlServer(
bool autoCreateSqlTable = false,
ColumnOptions columnOptions = null,
IConfigurationSection columnOptionsSection = null,
string schemaName = "dbo"
string schemaName = "dbo",
bool useMsi = false,
string azureServiceTokenProviderResource = null
)
{
if(loggerConfiguration == null)
if (useMsi && string.IsNullOrWhiteSpace(azureServiceTokenProviderResource))
throw new ArgumentNullException(nameof(azureServiceTokenProviderResource), "If useMsi is set to true, you must also provide an azureServiceTokenProviderResource");

if (loggerConfiguration == null)
throw new ArgumentNullException("loggerConfiguration");

var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
var colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
var tokenResource = useMsi ? ApplyMicrosoftExtensionsConfiguration.GetAzureServiceTokenProviderResource(azureServiceTokenProviderResource, appConfiguration) : null;

return loggerConfiguration.Sink(
new MSSqlServerSink(
Expand All @@ -78,7 +86,9 @@ public static LoggerConfiguration MSSqlServer(
formatProvider,
autoCreateSqlTable,
colOpts,
schemaName
schemaName,
useMsi,
tokenResource
),
restrictedToMinimumLevel);
}
Expand All @@ -96,6 +106,8 @@ public static LoggerConfiguration MSSqlServer(
/// <param name="columnOptions">An externally-modified group of column settings</param>
/// <param name="columnOptionsSection">A config section defining various column settings</param>
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
/// <param name="useMsi">Option to use MSI</param>
/// <param name="azureServiceTokenProviderResource">Resource required in AzureServiceTokenProvider.GetAccessTokenAsync(azureServiceTokenProviderResource). This will error if null, and useMsi is st to true</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration MSSqlServer(
Expand All @@ -108,23 +120,31 @@ public static LoggerConfiguration MSSqlServer(
bool autoCreateSqlTable = false,
ColumnOptions columnOptions = null,
IConfigurationSection columnOptionsSection = null,
string schemaName = "dbo"
string schemaName = "dbo",
bool useMsi = false,
string azureServiceTokenProviderResource = null
)
{
if(loggerAuditSinkConfiguration == null)
if (useMsi && string.IsNullOrWhiteSpace(azureServiceTokenProviderResource))
throw new ArgumentNullException(nameof(azureServiceTokenProviderResource), "If useMsi is set to true, you must also provide an azureServiceTokenProviderResource");

if (loggerAuditSinkConfiguration == null)
throw new ArgumentNullException("loggerAuditSinkConfiguration");

var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
var colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
var tokenResource = useMsi ? ApplyMicrosoftExtensionsConfiguration.GetAzureServiceTokenProviderResource(azureServiceTokenProviderResource, appConfiguration) : null;

return loggerAuditSinkConfiguration.Sink(
new MSSqlServerAuditSink(
connectionString,
connectionStr,
tableName,
formatProvider,
autoCreateSqlTable,
columnOptions,
schemaName
colOpts,
schemaName,
useMsi,
tokenResource
),
restrictedToMinimumLevel);
}
Expand Down
Loading