博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dapper扩展SQL跟踪及全局缓存通知-日志入队列
阅读量:7076 次
发布时间:2019-06-28

本文共 5651 字,大约阅读时间需要 18 分钟。

接上一篇:

记录Sql执行日志,主要处理  BeforeCommandExecute 方法

1  ///  2         /// 执行前事件跟踪信息 3         ///  4         /// 跟踪信息 5         private static void BeforeCommandExecute(TraceInfo traceInfo) 6         { 7             if (null != CurrentEvent && CurrentEvent.IsCancel == false) // 8             { 9                  CurrentEvent.FireBeforeExecute(GetNewTraceItem(traceInfo, false, false));10             }11              12         }

FireAfterExecute 这是一个订阅事件,它会把通过异步线程的试把消息提交到后台的队列,

缓存队列目前支持四种实现方式 ,可自由切换,在当前的框架里已经实现了 ASPNET, Redis, RabbitMQ 三种方式的队列,

默认采用的是Redis记录Sql,支持集中写入或单机写入模式,同时支持ASPNET单机写入模式(因为有的开发机器没有安装任何Redis, RabbitMQ, MongoDb之类的)

一、缓存队列接口设计,采用双Key机制,主要是支持Redis hashKey模式。在每个线程类,可按需启动一个线程来定期调度或进行事件订阅,

从缓存或队列中获取数据信息,再写入数据库或本地文件或通知 logstash来进行日志收集(下一步要做的)

 

1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Threading.Tasks;  6 using PlatForm;  7   8 namespace PlatForm.Caches  9 { 10     ///  11     /// 缓存与消息队列接口 12     ///  13     public interface ICacheAndQueue:IDisposable 14     { 15         ///  16         /// 获取缓存数据 17         ///  18         /// 分组Key 19         /// 缓存子Key 20         /// 为了提升性能,默认在30秒内使用本地缓存,不直接从Redis里提取,默认使用本地缓存 21      /// 是否用完就清除 22         /// 
23 List
GetCache(string groupKey, string key,bool isUseLocalCache=true,bool isRemoved=false); 24 25 ///
26 /// 设置缓存 27 /// 28 ///
缓存分组Key 29 ///
子Key 30 ///
数据 31 void SetCache(string groupKey, string key, List
data); 32 ///
33 /// 根据分组Key及子Key来进行删除缓存操作 34 /// 35 ///
分组Key 36 ///
子Key 37 ///
38 bool RemoveCache(string groupKey, string key); 39 ///
40 /// 删除整个分组及其下性的缓存信息 41 /// 42 ///
43 ///
44 bool RemoveCacheAll(string groupKey); 45 Dictionary
GetCacheCount(); 46 ///
47 /// 当前分组下的缓存是否存在 48 /// 49 ///
分组Key 50 ///
缓存子Key 51 ///
52 bool IsExsit(string goupKey, string key); 53 ///
54 /// 返回当前缓存组的所有缓存 55 /// 56 ///
57 ///
返回数据后,是否立即汪空缓存,默认不为清空 58 ///
是否为实体行数据 59 ///
60 List
GetCacheAll(string groupKey, bool isRemoveAll = false, bool isEntityRow = true); 61 ///
62 /// 得到当前分组下的缓存数 63 /// 64 ///
65 ///
66 int GetChildCount(string groupKey); 67 ///
68 /// 检测当前缓存是否正在正常运行 69 /// 70 ///
返回信息 71 ///
72 bool CheckCacheRunning(out string message); 73 long Publish(string channel, string message); 74 void Subscribe(string channelKey, Action
callBack); 75 ///
76 /// 入队操作 77 /// 78 ///
分组Key 79 ///
缓存子Key 80 ///
81 void EnqueenMessage(string groupKey, string key, EntityRow data); 82 ///
83 /// 出队操作 84 /// 85 ///
分组Key 86 ///
缓存子Key 87 ///
每次出队的个数 88 ///
89 List
DequeenMessages(string groupKey, string key, int size); 90 91 ///
92 /// 缓存队列类型 93 /// 94 CacheType CacheType { get; } 95 96 ///
97 /// 递增入队,用来统计点击量或访问量 98 /// 99 ///
分组Key100 ///
缓存子Key101 ///
102 void HasIncrement(string groupKey, string key );103 }104 }

 

二、缓存队列调用

 

其它附加类

1  ///  2         /// 格式化输出跟踪信息 3         ///  4         /// 跟踪信息 5         /// 是否结束 6         /// 是否发生错误 7         /// 
8 private static TraceInfoItem GetNewTraceItem(TraceInfo traceInfo,bool isEnd,bool isError) 9 {10 var parrams = traceInfo.SqlParams as DynamicParameters;11 if (null==parrams|| parrams.ParameterNames.Contains(Consts_DataTrace.DataCommandTraceID) == false)12 return null;13 string traceid = parrams.Get
(Consts_DataTrace.DataCommandTraceID);14 string contentId = parrams.Get
(Consts_DataTrace.DataCommandTraceContextID);15 string message = parrams.Get
(Consts_DataTrace.DataCommandTraceMsg);16 if (isError)17 {18 return new TraceInfoItem()19 {20 ID = traceid,21 Token = traceid,22 Body = FormatSql(traceInfo.CommandText, parrams),23 EndTime = traceInfo.ExecuteTime,24 Tag = message,25 IsError = true,26 };27 }28 29 if(isEnd)30 {31 return new TraceInfoItem()32 {33 ID = traceid,34 Token = traceid,35 36 EndTime = traceInfo.ExecuteTime,37 Tag = message,38 IsError = false,39 40 };41 42 }43 return new TraceInfoItem()44 {45 ID = traceid,46 Token = traceid,47 Body = FormatSql(traceInfo.CommandText, parrams),48 StartTime = traceInfo.ExecuteTime,49 Tag = message,50 IsError = true,51 52 };53 }

 

转载于:https://www.cnblogs.com/XSpots/p/7741584.html

你可能感兴趣的文章
Xcode:只修改 Bundle Identifier,不修改项目名
查看>>
《CLR via C#》读书笔记 之 计算限制的异步操作
查看>>
ADB环境变量的配置
查看>>
C primer plus 练习题 第七章
查看>>
virtualbox 创建com对象失败
查看>>
几种常见模式识别算法整理和总结
查看>>
Sort Colors leetcode java
查看>>
Length of Last Word leetocde java
查看>>
sharepoint项目遇到的WebDAV和HTTP PUT的安全隐患解决办法
查看>>
JAVA Metrics度量工具 - Metrics Core 翻译
查看>>
汇编之FS段寄存器
查看>>
设计模式(十):Decorator装饰者模式 -- 结构型模式
查看>>
Orchard Application Host
查看>>
iOS 消息转发
查看>>
CentOS修改TimeZone
查看>>
DIOCP3-DIOCP1升级到DIOCP3
查看>>
SQL Server 中WITH (NOLOCK)浅析
查看>>
09网易校园招聘笔试题
查看>>
。一个通俗易懂的HMM例子
查看>>
freeswitch 挂断前执行脚本
查看>>