Redis处理文件日志并发(2)
public class MyExceptionAttribute : HandleErrorAttribute
{
public static IRedisClientsManager ClientManager = new PooledRedisClientManager(new string[] {"127.0.0.1:6379"});
public static IRedisClient RedisClient = ClientManager.GetClient();
public override void OnException(ExceptionContext filterContext)
{
RedisClient.EnqueueItemOnList("errorException", filterContext.Exception.ToString());//将异常信息存储到Redis队列中了。
filterContext.HttpContext.Response.Redirect("/error.html");
base.OnException(filterContext);
}
}
[MyException]
public class HomeController : Controller
{
//
// GET: /Default1/ public ActionResult Index()
{
int a = ;
int b = ;
int c = a / b;
return View();
} }
Global文件
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); //通过线程开启一个线程,然后不停的从队列中或数据
string filePath = Server.MapPath("/Log/");
ThreadPool.QueueUserWorkItem(x =>
{
while (true)
{
try
{ if (MyExceptionAttribute.RedisClient.GetListCount("errorException") > )
{
string errorMsg = MyExceptionAttribute.RedisClient.DequeueItemFromList("errorException");//从Redis队列中取出异常数据
if(!string.IsNullOrEmpty(errorMsg))
{
//构建成一个完整的路径
string fileName = filePath + DateTime.Now.ToString("yyyy-MM-dd").ToString() + ".txt";
File.AppendAllText(fileName, errorMsg, Encoding.Default);//将异常写到文件中。
}
else
{
Thread.Sleep();
}
}
else
{
Thread.Sleep();//避免了CPU空转。
}
}
catch (Exception ex)
{ MyExceptionAttribute.RedisClient.EnqueueItemOnList("errorException", ex.ToString());
}
}
});
}
Redis处理文件日志并发(2)的更多相关文章
- 【分享】我们用了不到200行代码实现的文件日志系统,极佳的IO性能和高并发支持,附压力测试数据
很多项目都配置了日志记录的功能,但是,却只有很少的项目组会经常去看日志.原因就是日志文件生成规则设置不合理,将严重的错误日志跟普通的错误日志混在一起,分析起来很麻烦. 其实,我们想要的一个日志系统核心 ...
- PHP中使用Redis接管文件存储Session详解
前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用redis替换文件来存储session. 最近就遇到了这个问题,之前找了网上的一套直播 ...
- 【转载】redis.conf文件详解
转载地址:http://blog.csdn.net/zhutulang/article/details/51969760 Redis.conf文件可以在github上查看,下面是我整理的其中的配置项( ...
- PHP中如何使用Redis接管文件存储Session详解
https://www.jb51.net/article/151580.htm 前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用red ...
- redis学习(二)redis.conf文件配置
转自: https://www.cnblogs.com/pqchao/p/6558688.html 为了更好的使用redis,我们需要详细的了解redis配置文件及相关参数作用. bind 127.0 ...
- redis慢查询日志
运维需要记录一下主redis中那些“慢操作”的命令,然后找到相关的业务方,不然的话,阻塞 就不好玩了.然后就直接在redis手册中就找到了相关的命令. SLOWLOG subcommand [argu ...
- ELK+redis搭建nginx日志分析平台
ELK+redis搭建nginx日志分析平台发表于 2015-08-19 | 分类于 Linux/Unix | ELK简介ELKStack即Elasticsearch + Logstas ...
- 使用elk+redis搭建nginx日志分析平台
elk+redis 搭建nginx日志分析平台 logstash,elasticsearch,kibana 怎么进行nginx的日志分析呢?首先,架构方面,nginx是有日志文件的,它的每个请求的状态 ...
- redis的redis.conf文件详解
常用的: GENERAL: daemonize yes 守护进程 port 6379 指定Redis监听端口 requirepass 1 设置认证密码为1 REPLICATION: slave ...
随机推荐
- 跟着百度学PHP[4]-OOP面对对象编程-2-属性和方法
简单的说 变量就是成员属性函数就是成员方法(方法有三:构造方法[即为__construct].成员方法.析构方法[__destruct]) 成员方法和成员属性都是可以加修饰词.比如封装性的方法或者属性 ...
- (转载)XML解析之-XStream解析
转载来源:http://hwy584624785.iteye.com/blog/1168680 本例使用XStream生成一个xml文件,再发序列化xml文件内容. XStream是一个简单的类库,可 ...
- echarts之字符云tooltip显示混乱问题的解决办法
echarts字符云中tooltip显示混乱主要表现为一下两点: 1.字体与其显示框内容不对应鼠标识别错误 解决思路: 就是option里的数据要对value降序排序(这一点很关键,是必须的一步) 把 ...
- Validform —— 再也不用担心“表单验证”!
<!doctype html> <html> <head> <meta content="text/html" charset=" ...
- ubuntu安装skype
1.添加源 sudo add-apt-repository "deb http://archive.canonical.com/ $(lsb_release -sc) partner&quo ...
- freemodbus-v1.5.0 源码分析
注:转载请注明出处 http://www.cnblogs.com/wujing-hubei/p/5935142.html FreeModbus协议栈作为从机,等待主机传送的数据,当从机接收到一帧完 ...
- http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/
http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/ box-shadow : http://blog.csdn.net/freshlover/a ...
- MySQL ODBC for Linux
参考自http://blog.csdn.net/allens_zhou/article/details/8575400 centos7 64bit [IP:192.168.0.100] yum ins ...
- Light OJ 1068
数位DP #include <cstdio> #include <cstring> using namespace std; ; ; long long n; int f[MA ...
- question2answer论坛框架分析及web开发思考
2015年7月25日 17:31:42 星期六 一个专门做论坛的开源PHP框架, 有后台, 支持多语种 入口文件是框架根目录的index.php 他包含了/qa-include/qa-index.ph ...