Beispiel #1
0
        public static void BuildConnectionStringElementMatcher(this ConfigurationDiagnosticsOptions options,
                                                               Action <IKeyMatchBuilder> builder)
        {
            var builtMatcher = BuildMatcher(builder);

            options.ConnectionStringElementMatcher = builtMatcher;
        }
        private static void AddConnectionStringKeysAndValues(
            StringBuilder messageTemplate,
            DbConnectionStringBuilder builder,
            List <object> args,
            ConfigurationDiagnosticsOptions options)
        {
            var keys  = builder.Keys ?? Array.Empty <string>();
            int index = 0;

            foreach (var keyObj in keys)
            {
                string key = (string)keyObj;
                messageTemplate.AppendLine(" * {key" + index + "} = {value" + index + "}");
                args.Add(keyObj);
                var value =
                    options.ConnectionStringElementMatcher.IsMatch(key)
                        ? options.Obfuscator.Obfuscate((string)builder[key])
                        : (string)builder[key];
                args.Add(value);
            }
        }
        /// <summary>
        /// Logs the keys and values in the given configuration object.
        /// </summary>
        /// <param name="config">The configuration object to examine.</param>
        /// <param name="logger">The logger to write the results to.</param>
        /// <param name="level">The level to log at.</param>
        /// <param name="options">The options to use to obfuscate secrets.</param>
        public static void LogConfigurationValues(this ILogger logger, IConfiguration config, LogLevel level, ConfigurationDiagnosticsOptions options)
        {
            var valueMap = config.AsEnumerable()
                           .OrderBy(kvp => kvp.Key)
                           .Select(kvp => Obfuscate(kvp, options))
                           .Select(Render);
            string message = "The following values are available:" +
                             Environment.NewLine +
                             string.Join(Environment.NewLine, valueMap);

            logger.Log(level, message);
        }
 /// <summary>
 /// Logs the keys and values in the given configuration object at the trace level.
 /// </summary>
 /// <param name="config">The configuration object to examine.</param>
 /// <param name="logger">The logger to write the results to.</param>
 /// <param name="options">The options to use to obfuscate secrets.</param>
 public static void LogConfigurationValuesAsTrace(this ILogger logger, IConfiguration config, ConfigurationDiagnosticsOptions options)
 {
     logger.LogConfigurationValues(config, LogLevel.Trace, options);
 }
 /// <summary>
 /// Logs the keys and values in the given configuration object at the information level.
 /// </summary>
 /// <param name="config">The configuration object to examine.</param>
 /// <param name="logger">The logger to write the results to.</param>
 /// <param name="options">The options to use to obfuscate secrets.</param>
 public static void LogConfigurationValuesAsInformation(this ILogger logger, IConfiguration config, ConfigurationDiagnosticsOptions options)
 {
     logger.LogConfigurationValues(config, LogLevel.Information, options);
 }
 private static KeyValuePair <string, string> Obfuscate(KeyValuePair <string, string> kvp, ConfigurationDiagnosticsOptions options)
 {
     return(options.ConfigurationKeyMatcher.IsMatch(kvp.Key)
         ? new KeyValuePair <string, string>(kvp.Key, options.Obfuscator.Obfuscate(kvp.Value))
         : kvp);
 }
 /// <summary>
 /// Logs the details of the connection's connection string at the debug level.
 /// </summary>
 /// <param name="connection">The connection object.</param>
 /// <param name="logger">The logger to send the details to.</param>
 public static void LogConnectionStringAsDebug(this ILogger logger, IDbConnection connection, ConfigurationDiagnosticsOptions options = null)
 {
     logger.LogConnectionString(connection, LogLevel.Debug, options);
 }
        /// <summary>
        /// Logs the details of the connection's connection string at the given level.
        /// </summary>
        /// <param name="connection">The connection object.</param>
        /// <param name="logger">The logger to send the details to.</param>
        /// <param name="level">The level to log at.</param>
        public static void LogConnectionString(this ILogger logger, IDbConnection connection, LogLevel level, ConfigurationDiagnosticsOptions options = null)
        {
            var connectionString = connection.ConnectionString;

            logger.LogConnectionString(level, connectionString, null, options);
        }
 /// <summary>
 /// Logs the details of the named connection string at the trace level.
 /// </summary>
 /// <param name="config">The configuration that contains the connection strings.</param>
 /// <param name="name">The name of the connection string to log.</param>
 /// <param name="logger">The logger to send the details to.</param>
 public static void LogConnectionStringAsTrace(this ILogger logger, IConfiguration config, string name, ConfigurationDiagnosticsOptions options)
 {
     logger.LogConnectionString(config, name, LogLevel.Trace, options);
 }
 /// <summary>
 /// Logs the details of the named connection string at the information level.
 /// </summary>
 /// <param name="config">The configuration that contains the connection strings.</param>
 /// <param name="name">The name of the connection string to log.</param>
 /// <param name="logger">The logger to send the details to.</param>
 public static void LogConnectionStringAsInformation(this ILogger logger, IConfiguration config, string name, ConfigurationDiagnosticsOptions options = null)
 {
     logger.LogConnectionString(config, name, LogLevel.Information, options);
 }
        /// <summary>
        /// Logs the details of the named connection string at the desired level.
        /// </summary>
        /// <param name="config">The configuration that contains the connection strings.</param>
        /// <param name="name">The name of the connection string to log.</param>
        /// <param name="logger">The logger to send the details to.</param>
        /// <param name="level">The level to log at.</param>
        public static void LogConnectionString(this ILogger logger, IConfiguration config, string name, LogLevel level, ConfigurationDiagnosticsOptions options = null)
        {
            string connectionString = config.GetConnectionString(name);

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                logger.Log(
                    level,
                    "There is no connection string in the configuration with the name {name}",
                    name);
            }
            else
            {
                logger.LogConnectionString(level, connectionString, name, options);
            }
        }
        /// <summary>
        /// Logs the deconstructed connection string at the desired level.
        /// </summary>
        /// <param name="logger">The logger to send the details to.</param>
        /// <param name="level">The level to log at.</param>
        /// <param name="connectionString">The connection string to be deconstructed.</param>
        /// <param name="connectionStringName">The name of the connection string, or null if there is no name or it is not known.</param>
        public static void LogConnectionString(this ILogger logger, LogLevel level, string connectionString, string connectionStringName = null, ConfigurationDiagnosticsOptions options = null)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                logger.Log(level, "No connection string to log.");
                return;
            }

            options = options ?? ConfigurationDiagnosticsOptions.GlobalOptions;

            DbConnectionStringBuilder builder = new DbConnectionStringBuilder();

            builder.ConnectionString = connectionString;

            List <object> args            = new List <object>();
            StringBuilder messageTemplate = new StringBuilder(connectionString.Length);

            BuildStartOfMessage(messageTemplate, connectionStringName, args);
            AddConnectionStringKeysAndValues(messageTemplate, builder, args, options);

            var objArgs = args.ToArray();

            logger.Log(level, messageTemplate.ToString(), objArgs);
        }