|
| 1 | +// Copyright 2014 Serilog Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using Serilog.Configuration; |
| 17 | +using Serilog.Events; |
| 18 | +using Serilog.Sinks.MSSqlServer; |
| 19 | +using Microsoft.Extensions.Configuration; |
| 20 | +using System.Configuration; |
| 21 | +using Serilog.Debugging; |
| 22 | + |
| 23 | +// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration. |
| 24 | +// This is necessary because .NET Framework 4.6.1+ and .NET Core 2.0+ apps support both approaches, whereas the |
| 25 | +// older .NET Framework 4.5.2 only supports System.Configuration and .NET Standard 2.0 only supports M.E.C. |
| 26 | + |
| 27 | +namespace Serilog |
| 28 | +{ |
| 29 | + /// <summary> |
| 30 | + /// Adds the WriteTo.MSSqlServer() extension method to <see cref="LoggerConfiguration"/>. |
| 31 | + /// </summary> |
| 32 | + public static class LoggerConfigurationMSSqlServerExtensions |
| 33 | + { |
| 34 | + /// <summary> |
| 35 | + /// The configuration section name for app.config or web.config configuration files. |
| 36 | + /// </summary> |
| 37 | + public static string AppConfigSectionName = "MSSqlServerSettingsSection"; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Adds a sink that writes log events to a table in a MSSqlServer database. |
| 41 | + /// Create a database and execute the table creation script found here |
| 42 | + /// https://gist.github.com/mivano/10429656 |
| 43 | + /// or use the autoCreateSqlTable option. |
| 44 | + /// </summary> |
| 45 | + /// <param name="loggerConfiguration">The logger configuration.</param> |
| 46 | + /// <param name="connectionString">The connection string to the database where to store the events.</param> |
| 47 | + /// <param name="tableName">Name of the table to store the events in.</param> |
| 48 | + /// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param> |
| 49 | + /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> |
| 50 | + /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> |
| 51 | + /// <param name="period">The time to wait between checking for event batches.</param> |
| 52 | + /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> |
| 53 | + /// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param> |
| 54 | + /// <param name="columnOptions">An externally-modified group of column settings</param> |
| 55 | + /// <param name="columnOptionsSection">A config section defining various column settings</param> |
| 56 | + /// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param> |
| 57 | + /// <returns>Logger configuration, allowing configuration to continue.</returns> |
| 58 | + /// <exception cref="ArgumentNullException">A required parameter is null.</exception> |
| 59 | + public static LoggerConfiguration MSSqlServer( |
| 60 | + this LoggerSinkConfiguration loggerConfiguration, |
| 61 | + string connectionString, |
| 62 | + string tableName, |
| 63 | + IConfiguration appConfiguration = null, |
| 64 | + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, |
| 65 | + int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit, |
| 66 | + TimeSpan? period = null, |
| 67 | + IFormatProvider formatProvider = null, |
| 68 | + bool autoCreateSqlTable = false, |
| 69 | + ColumnOptions columnOptions = null, |
| 70 | + IConfigurationSection columnOptionsSection = null, |
| 71 | + string schemaName = "dbo" |
| 72 | + ) |
| 73 | + { |
| 74 | + if(loggerConfiguration == null) |
| 75 | + throw new ArgumentNullException("loggerConfiguration"); |
| 76 | + |
| 77 | + var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod; |
| 78 | + var colOpts = columnOptions ?? new ColumnOptions(); |
| 79 | + var connStr = connectionString; |
| 80 | + |
| 81 | + if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection) |
| 82 | + { |
| 83 | + colOpts = ApplySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts); |
| 84 | + connStr = ApplySystemConfiguration.GetConnectionString(connStr); |
| 85 | + |
| 86 | + if (appConfiguration != null || columnOptionsSection != null) |
| 87 | + SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink."); |
| 88 | + } |
| 89 | + |
| 90 | + if (appConfiguration != null || columnOptionsSection != null) |
| 91 | + { |
| 92 | + connStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration); |
| 93 | + colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection); |
| 94 | + } |
| 95 | + |
| 96 | + return loggerConfiguration.Sink( |
| 97 | + new MSSqlServerSink( |
| 98 | + connStr, |
| 99 | + tableName, |
| 100 | + batchPostingLimit, |
| 101 | + defaultedPeriod, |
| 102 | + formatProvider, |
| 103 | + autoCreateSqlTable, |
| 104 | + colOpts, |
| 105 | + schemaName |
| 106 | + ), |
| 107 | + restrictedToMinimumLevel); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Adds a sink that writes log events to a table in a MSSqlServer database. |
| 112 | + /// </summary> |
| 113 | + /// <param name="loggerAuditSinkConfiguration">The logger configuration.</param> |
| 114 | + /// <param name="connectionString">The connection string to the database where to store the events.</param> |
| 115 | + /// <param name="tableName">Name of the table to store the events in.</param> |
| 116 | + /// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param> |
| 117 | + /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> |
| 118 | + /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> |
| 119 | + /// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param> |
| 120 | + /// <param name="columnOptions">An externally-modified group of column settings</param> |
| 121 | + /// <param name="columnOptionsSection">A config section defining various column settings</param> |
| 122 | + /// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param> |
| 123 | + /// <returns>Logger configuration, allowing configuration to continue.</returns> |
| 124 | + /// <exception cref="ArgumentNullException">A required parameter is null.</exception> |
| 125 | + public static LoggerConfiguration MSSqlServer( |
| 126 | + this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration, |
| 127 | + string connectionString, |
| 128 | + string tableName, |
| 129 | + IConfiguration appConfiguration = null, |
| 130 | + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, |
| 131 | + IFormatProvider formatProvider = null, |
| 132 | + bool autoCreateSqlTable = false, |
| 133 | + ColumnOptions columnOptions = null, |
| 134 | + IConfigurationSection columnOptionsSection = null, |
| 135 | + string schemaName = "dbo" |
| 136 | + ) |
| 137 | + { |
| 138 | + if(loggerAuditSinkConfiguration == null) |
| 139 | + throw new ArgumentNullException("loggerAuditSinkConfiguration"); |
| 140 | + |
| 141 | + var colOpts = columnOptions ?? new ColumnOptions(); |
| 142 | + var connStr = connectionString; |
| 143 | + |
| 144 | + if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection) |
| 145 | + { |
| 146 | + colOpts = ApplySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts); |
| 147 | + connStr = ApplySystemConfiguration.GetConnectionString(connStr); |
| 148 | + |
| 149 | + if (appConfiguration != null || columnOptionsSection != null) |
| 150 | + SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink."); |
| 151 | + } |
| 152 | + |
| 153 | + if (appConfiguration != null || columnOptionsSection != null) |
| 154 | + { |
| 155 | + connStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration); |
| 156 | + colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection); |
| 157 | + } |
| 158 | + |
| 159 | + return loggerAuditSinkConfiguration.Sink( |
| 160 | + new MSSqlServerAuditSink( |
| 161 | + connStr, |
| 162 | + tableName, |
| 163 | + formatProvider, |
| 164 | + autoCreateSqlTable, |
| 165 | + colOpts, |
| 166 | + schemaName |
| 167 | + ), |
| 168 | + restrictedToMinimumLevel); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments