PooledRedisClient.cs
public class PooledRedisClient { private readonly string _baseUri; private readonly PooledRedisClientManager _clientManager; ////// 实例化连接池客户端 /// /// /// /// 默认值10 public PooledRedisClient(string host, int port, int maxPoolSize = 10) { _baseUri = string.Format("{0}:{1}", host, port); var cOnfig= new RedisClientManagerConfig(); config.MaxReadPoolSize = maxPoolSize; config.MaxWritePoolSize = maxPoolSize; _clientManager = new PooledRedisClientManager(new string[] { _baseUri }, new string[] { _baseUri }, config); } /// /// 异步记录 /// /// /// public void AddItemToListAsync(string listId, string log) { //使用Task任务异步执行 var task = new Task(() => { try { using (var clinet = _clientManager.GetClient()) { clinet.AddItemToList(listId, log); } } catch (Exception ex) { Trace.WriteLine(ex.Message); } }); task.Start(); } }
在RedisClient连接时用了连接池,写日志时用Task做了异步。这种写法不知道会不会存在问题?!
配置文件
xml version="1.0"?> <log4net> <appender name="redisAppender" type="log4net.Redis.Appender.RedisAppender, log4net.Redis"> <Host value="192.168.0.10" /> <Port value="6379" /> <MaxPoolSize value="500" /> <ListId value="logstash" /> <layout type="log4net.Redis.Layout.JsonLayout,log4net.Redis" > <LogType value="iis_log" /> <AppName value="Demo" /> layout> appender> <root> <level value="ALL" /> <appender-ref ref="redisAppender" /> root> log4net>
Layout和Appender里的属性配置和代码里属性配置名称一致,Log4net的框架就可以读取配置数据反射到实体属性上。
代码下载地址:
http://download.csdn.net/detail/zbl131/7702673
参考文章:
1.用Kibana和logstash快速搭建实时日志查询、收集与分析系统
http://storysky.blog.51cto.com/628458/1158707/
2.使用ServiceStackRedis链接Redis简介
http://www.cnblogs.com/daizhj/archive/2011/02/17/1956860.html
3.ServiceStack.Redis的问题与修正
http://blog.csdn.net/susubuhui/article/details/8930417
4.对ServiceStack.Redis的连接池进行故障转移改造
http://www.cnblogs.com/smark/archive/2013/05/24/3096488.html