App.Config自定义配置节点

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="redisConfiguration" type="Redis.Configuration.RedisSettings,Redis,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>
<redisConfiguration db="0" writeServerConStr="192.168.10.9:6379" readServerConStr="192.168.10.9:6379" maxWritePoolSize="100" maxReadPoolSize="100" autoStart="true" localCacheTime="31536000" recordeLog="false" />
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>

自定义节点名称

       /// <summary>
/// sectionName
/// </summary>
/// <param name="sectionName">sectionName</param>
/// <returns>RedisSettings</returns>
public static RedisSettings GetConfig(string sectionName)
{
RedisSettings section = (RedisSettings)ConfigurationManager.GetSection("redisConfiguration"/*自定义节点名称*/);
if (section == null)
{
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
}
return section;
}

RedisSettings 配置类

    /// <summary>
/// RedisSettings
/// </summary>
public sealed class RedisSettings : ConfigurationSection
{
/// <summary>
/// GetConfig
/// </summary>
/// <returns>RedisSettings</returns>
public static RedisSettings GetConfig()
{
RedisSettings section = GetConfig(RedisMappingConstants.RedisConfiguration);
return section;
} /// <summary>
/// sectionName
/// </summary>
/// <param name="sectionName">sectionName</param>
/// <returns>RedisSettings</returns>
public static RedisSettings GetConfig(string sectionName)
{
RedisSettings section = (RedisSettings)ConfigurationManager.GetSection(sectionName);
if (section == null)
{
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
}
return section;
} /// <summary>
/// DB
/// </summary>
[ConfigurationProperty(RedisMappingConstants.DbAttributeName, IsKey = true, IsRequired = true)]
public int Db
{
get
{
return (int)this[RedisMappingConstants.DbAttributeName];
} set
{
this[RedisMappingConstants.DbAttributeName] = value;
}
} /// <summary>
/// 可写的Redis链接地址
/// </summary>
[ConfigurationProperty(RedisMappingConstants.WriteServerConStrAttributeName, IsRequired = true)]
public string WriteServerConStr
{
get
{
return (string)this[RedisMappingConstants.WriteServerConStrAttributeName];
} set
{
this[RedisMappingConstants.WriteServerConStrAttributeName] = value;
}
} /// <summary>
/// 可读的Redis链接地址
/// </summary>
[ConfigurationProperty(RedisMappingConstants.ReadServerConStrAttributeName, IsRequired = true)]
public string ReadServerConStr
{
get
{
return (string)this[RedisMappingConstants.ReadServerConStrAttributeName];
} set
{
this[RedisMappingConstants.ReadServerConStrAttributeName] = value;
}
} /// <summary>
/// 最大写链接数
/// </summary>
[ConfigurationProperty(RedisMappingConstants.MaxWritePoolSizeAttributeName, IsRequired = true)]
public int MaxWritePoolSize
{
get
{
return (int)this[RedisMappingConstants.MaxWritePoolSizeAttributeName];
} set
{
this[RedisMappingConstants.MaxWritePoolSizeAttributeName] = value;
}
} /// <summary>
/// 最大写链接数
/// </summary>
[ConfigurationProperty(RedisMappingConstants.MaxReadPoolSizeAttributeName, IsRequired = true)]
public int MaxReadPoolSize
{
get
{
return (int)this[RedisMappingConstants.MaxReadPoolSizeAttributeName];
} set
{
this[RedisMappingConstants.MaxReadPoolSizeAttributeName] = value;
}
} /// <summary>
/// 自动重启
/// </summary>
[ConfigurationProperty(RedisMappingConstants.AutoStartAttributeName, IsRequired = true)]
public bool AutoStart
{
get
{
return (bool)this[RedisMappingConstants.AutoStartAttributeName];
} set
{
this[RedisMappingConstants.AutoStartAttributeName] = value;
}
} /// <summary>
/// 本地缓存到期时间,单位:秒
/// </summary>
[ConfigurationProperty(RedisMappingConstants.LocalCacheTimeAttributeName, IsRequired = true)]
public int LocalCacheTime
{
get
{
return (int)this[RedisMappingConstants.LocalCacheTimeAttributeName];
} set
{
this[RedisMappingConstants.LocalCacheTimeAttributeName] = value;
}
} /// <summary>
/// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
/// </summary>
[ConfigurationProperty(RedisMappingConstants.RecordeLogAttributeName, IsRequired = true)]
public bool RecordeLog
{
get
{
return (bool)this[RedisMappingConstants.RecordeLogAttributeName];
} set
{
this[RedisMappingConstants.RecordeLogAttributeName] = value;
}
}
}

RedisMappingConstants 属性类

    /// <summary>
/// 配置节点名称
/// </summary>
public class RedisMappingConstants
{
/// <summary>
/// 配置文件映射节点
/// </summary>
public const string RedisConfiguration = "redisConfiguration"; /// <summary>
/// 数据库实例名称
/// </summary>
public const string DbAttributeName = "db"; /// <summary>
/// 可写的Redis链接地址
/// </summary>
public const string WriteServerConStrAttributeName = "writeServerConStr"; /// <summary>
/// 可读的Redis链接地址
/// </summary>
public const string ReadServerConStrAttributeName = "readServerConStr"; /// <summary>
/// 最大写链接数
/// </summary>
public const string MaxWritePoolSizeAttributeName = "maxWritePoolSize"; /// <summary>
/// 最大读链接数
/// </summary>
public const string MaxReadPoolSizeAttributeName = "maxReadPoolSize"; /// <summary>
/// 自动重启
/// </summary>
public const string AutoStartAttributeName = "autoStart"; /// <summary>
/// 本地缓存到期时间,单位:秒
/// </summary>
public const string LocalCacheTimeAttributeName = "localCacheTime"; /// <summary>
/// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
/// </summary>
public const string RecordeLogAttributeName = "recordeLog";
}
上一篇:使用VUE的准备工作,以及显示文本


下一篇:sed与grep正则