分布式日志2 用redis的队列写日志
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceStack.Redis;
namespace 分布式日志
{
public class MyExceptionFilter : HandleErrorAttribute
{
#region 用c#的队列
//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);
//}
#endregion
#region 用redis的队列
public static IRedisClientsManager clientsManager=new PooledRedisClientManager(new string[]{"127.0.0.1:6379"});
public static IRedisClient redisClient=clientsManager.GetClient();
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception != null)
{
redisClient.EnqueueItemOnList("exception",filterContext.Exception.ToString());//入队
filterContext.HttpContext.Response.Redirect("/error.html");
}
base.OnException(filterContext);
}
#endregion
}
}
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)
{
#region c# 队列
//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);
//}
#endregion
#region redis 队列
string strKey = "exception";
try
{
)
{
string strMsg = MyExceptionFilter.redisClient.DequeueItemFromList(strKey);
if (!string.IsNullOrEmpty(strMsg))
{
System.IO.File.AppendAllText(strPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + strMsg + Environment.NewLine, System.Text.Encoding.UTF8);
}
else
{
Thread.Sleep();
}
}
else
{
Thread.Sleep();//避免cpu空转
}
}
catch (Exception ex)
{
MyExceptionFilter.redisClient.EnqueueItemOnList(strKey, ex.ToString());
}
#endregion
}
}, strPath);
}
}
}
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());
}
}
}
分布式日志2 用redis的队列写日志的更多相关文章
- 分布式日志1 用c#的队列写日志
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 警告 ...
- Redis 自定义 RedisAppender 插件, 实现日志缓冲队列,集中日志输出.
因为某些异步日志设置了即使队列满了,也不可丢弃,在并发高的时候,导致请求方法同步执行,响应变慢. 编写这个玩意,除了集中日志输出以外,还希望在高并发的时间点有缓冲作用. 之前用Kafka实现了一次入队 ...
- 利用StackExchange.Redis和Log4Net构建日志队列
简介:本文是一个简单的demo用于展示利用StackExchange.Redis和Log4Net构建日志队列,为高并发日志处理提供一些思路. 0.先下载安装Redis服务,然后再服务列表里启动服务(R ...
- java-spring基于redis单机版(redisTemplate)实现的分布式锁+redis消息队列,可用于秒杀,定时器,高并发,抢购
此教程不涉及整合spring整合redis,可另行查阅资料教程. 代码: RedisLock package com.cashloan.analytics.utils; import org.slf4 ...
- c# 多线程使用队列顺序写日志的类 (需要再优化)
using System; using System.Collections.Generic; using System.Threading; public class LogManager { // ...
- Docker部署Django项目+Nginx+Fluend日志收集 和redis、memcached、RabbitMQ、Celery
前言 一.docker 1.docker是什么? Docker的英文本意是“搬运工”,Docker搬运的是集装箱(Container)可以成为容器,我可以把写的Django的WEB应用以及Python ...
- ELK(+Redis)-开源实时日志分析平台
################################################################################################### ...
随机推荐
- Oracle导入和导出
导出:EXP userid=<username>/<password>@<service_name> file=<dmpname> e.g.exp sa ...
- LayoutInflater.java (android-19)
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- ios中strong和weak的解释理解
来自stackoverflow解释的挺有意思的 Imagine our object is a dog, and that the dog wants to run away (be dealloca ...
- HTC Vive开发笔记之SteamVR插件集成
重要组件 SteamVR_Camera VR摄像机,主要功能是将Unity摄像机的画面进行变化,形成Vive中的成像画面 使用方法: l 在任一个摄像机上增加脚本 l 点击Expand按钮 完成以上操 ...
- discuz论坛与其它网站登录注册整合
discuz论坛与其它网站登录注册整合 本文以discuz 7.0.0 php版本的论坛与 .net 2.0的网站注册登录整合为类.没有采用uc_center或第三方插件.以另类的方式实现.此方法实现 ...
- 1. Longest Palindromic Substring ( 最长回文子串 )
要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...
- PDF 生成插件 flying saucer 和 iText
最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...
- dedecms 打印出网站所有 文章标题和链接(URL)的方法
{dede:arclist row="100000"} <li>[field:fulltitle/]***网站URL地址***[field:arcurl/]</l ...
- string.Format 格式化输出日期
string.Format("{0:d}",System.DateTime.Now) 结果为:2009-3-20 (月份位置不是03) string.Format("{0 ...
- 深入学习微框架:Spring Boot(转)
转:http://www.infoq.com/cn/articles/microframeworks1-spring-boot/ 相关参考: https://spring.io/guides/gs/s ...