分布式日志1 用c#的队列写日志
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace 分布式日志 { public class MyExceptionFilter : HandleErrorAttribute { public static Queue<Exception> listQueue = new Queue<Exception>(); public override void OnException(ExceptionContext filterContext) { if (filterContext.Exception!=null) { listQueue.Enqueue(filterContext.Exception); filterContext.HttpContext.Response.Redirect("/error.html"); } base.OnException(filterContext); } } }
using System.Web; using System.Web.Mvc; namespace 分布式日志 { public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //filters.Add(new HandleErrorAttribute()); filters.Add(new MyExceptionFilter()); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace 分布式日志 { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //通过线程池开启一个线程,然后不停的从队列中读取数据 string strRoot = Server.MapPath("/Log/"); string strPath = strRoot + DateTime.Now.ToString("yyyy-MM-dd").ToString()+".txt"; ThreadPool.QueueUserWorkItem(i => { while (true) { try { if (MyExceptionFilter.listQueue.Count > 0) { Exception ex = MyExceptionFilter.listQueue.Dequeue(); if (ex != null) { System.IO.File.AppendAllText( strPath,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ex.ToString() + Environment.NewLine,System.Text.Encoding.UTF8); } else { Thread.Sleep(30); } } else { Thread.Sleep(30);//避免cpu空转 } } catch(Exception ex) { MyExceptionFilter.listQueue.Enqueue(ex); } } }, strPath); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace 分布式日志.Controllers { public class TestController : Controller { // // GET: /Test/ public ActionResult Index() { int aa = Convert.ToInt32("sss"); return View(); } } }
分布式日志1 用c#的队列写日志的更多相关文章
- 分布式日志2 用redis的队列写日志
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 重复造轮子,编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)
一说到写日志,大家可能推荐一堆的开源日志框架,如:Log4Net.NLog,这些日志框架确实也不错,比较强大也比较灵活,但也正因为又强大又灵活,导致我们使用他们时需要引用一些DLL,同时还要学习各种用 ...
- python 14篇 写日志
import sys from loguru import logger # 日志级别: debug 调试信息打印日志比较详细,级别最低 # info 正常的提示信息,级别较低 # waring 警告 ...
- c# 多线程使用队列顺序写日志的类 (需要再优化)
using System; using System.Collections.Generic; using System.Threading; public class LogManager { // ...
- Redis 自定义 RedisAppender 插件, 实现日志缓冲队列,集中日志输出.
因为某些异步日志设置了即使队列满了,也不可丢弃,在并发高的时候,导致请求方法同步执行,响应变慢. 编写这个玩意,除了集中日志输出以外,还希望在高并发的时间点有缓冲作用. 之前用Kafka实现了一次入队 ...
- C# 超高速高性能写日志 代码开源
1.需求 需求很简单,就是在C#开发中高速写日志.比如在高并发,高流量的地方需要写日志.我们知道程序在操作磁盘时是比较耗时的,所以我们把日志写到磁盘上会有一定的时间耗在上面,这些并不是我们想看到的. ...
- logback KafkaAppender 写入Kafka队列,集中日志输出.
为了减少应用服务器对磁盘的读写,以及可以集中日志在一台机器上,方便使用ELK收集日志信息,所以考虑做一个jar包,让应用集中输出日志 网上搜了一圈,只发现有人写了个程序在github 地址:https ...
- [转]C# 超高速高性能写日志 代码开源
1.需求 需求很简单,就是在C#开发中高速写日志.比如在高并发,高流量的地方需要写日志.我们知道程序在操作磁盘时是比较耗时的,所以我们把日志写到磁盘上会有一定的时间耗在上面,这些并不是我们想看到的 ...
- 分布式计算 要不要把写日志独立成一个Server Remote Procedure Call Protocol
w https://en.wikipedia.org/wiki/Remote_procedure_call In distributed computing a remote procedure ca ...
随机推荐
- oracle数据库对象使用说明
1.创建一个分区表,并插入一些数据,同时查询出每个分区的数据. 答:创建分区表如下 2.创建一个视图,并给出一个查询语句. 3.在当前用户下创建一个同义词,用于查询scott用户下的dept表,并给出 ...
- REST风格URL
以前就是觉得 /nowamagic/article/article_id 这样的地址非常的漂亮,但是那只是表象罢了,了解深入以后,发现必须有一个客户端的Ajax Engine和Server端的服务配合 ...
- [CF245H] Queries for Number of Palindromes (容斥原理dp计数)
题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...
- uboot和内核波特率不同
uboot和内核波特率不同,在uboot启动后,修改uboot参数: set bootargs 'noinitrd root=/dev/mtdblock3 init=/linuxrc console= ...
- Django base view
class django.views.generic.base.View 它是基类的基类,其它View基类都是从这继承的. 官例: from django.http import HttpRespon ...
- JavaScript笔记基础篇(一)
一. 常用正则表达式汇总以及部分问题解决方案 正则匹配: var str = "This is my test"; var test = new RegExp("test ...
- 纸上谈兵:左倾堆(leftist heap)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们之前讲解了堆(heap)的概念.堆是一个优先队列.每次从堆中取出的元素都是堆中 ...
- sql条件中比较性能优化
第一个比第二个性能高. 查询语句意义: 如果codelist中tablecode配置为0时, t.Table_Code = 'SV_RETURN_BILL'不生效. 如果codelist中tablec ...
- java实验2实验报告(20135131)
一.实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计模式 二.实验要求 1.没有L ...
- 【转载】Fast Inserts to PostgreSQL with JDBC and COPY FROM
source: http://rostislav-matl.blogspot.com/2011/08/fast-inserts-to-postgresql-with-jdbc.html Thanks ...