分布式日志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 ...
随机推荐
- vim(5)vim下wimrc的配置,解决中文乱码问题
解决linux下vim乱码的情况:(修改vimrc的内容) 全局的情况下:即所有用户都能用这个配置 文件地址:/etc/vimrc 在文件中添加: ,ucs-bom,gb18030,gbk,gb231 ...
- 中大bbs
发现个好像很好玩的东东, 中大bbs: luit -encoding gbk telnet bbs.sysu.edu.cn
- TEST设置
- 【转】windows7 修改环境变量 和 用不用重启电脑的讨论
原文:http://www.cnblogs.com/zhenmingliu/archive/2013/02/21/2921396.html 先到我的电脑>属性>高级>环境变量 ...
- 静态修饰符(关键字static)
1.Static修饰的方法或变量通常称为类方法和类属性 2.静态方法中不能使用this和super关键字,也不能做为局部变量使用 3.在静态方法中不能访问非静态成员方法和非静态成员变量,但是在非静态成 ...
- winform画图闪烁问题
问题:在winform程序的onpaint方法中画图, 连续画, 如鼠标移动时就要不断画图, 会闪烁. 解决方法:将要画图的部分放到一个自定义控件中, 自定义控件的onpaint方法里面画图, 然后再 ...
- web测试一般分为那几个阶段,哪些阶段是可以用工具实现的,都有些什么工具,哪些阶段必须要人工手动来实现呢?
这是我在知乎上遇到的一个问题: web测试一般分为那几个阶段,哪些阶段是可以用工具实现的,都有些什么工具,哪些阶段必须要人工手动来实现呢? 首先这个提问本身就是有问题的, 没有哪个阶段是用工具实现的, ...
- ActionScript 3.0 for the Lunder Algorithm
package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalen ...
- 2016-08-15:从YUV420P中提取指定大小区域
typedef struct { int width; int height; }SizeInfo; typedef struct { int x; int y; int width; int hei ...
- NHibernate系列文章十七:NHibernate Session管理(附程序下载)
摘要 NHibernate的Session的管理涉及到NHibernate的两个最重要的对象ISessionFactory和ISession.ISessionFactory的生成非常消耗资源,通常都在 ...