首页 > 其他分享 >庆军之假wal系统的实现记录

庆军之假wal系统的实现记录

时间:2023-03-03 14:44:18浏览次数:34  
标签:wal Name 记录 currentFile 庆军 walloginfo ReWALDefine string

要解决的问题:

我有一个系统,但是数据库表示它不稳定了,我需要对外高可用。数据库不能用的时候,总不能不用吧。所以,只能特征一致性。

临时的数据存在文件wal里面,由另一个线程单独处理。

原来是打算用 faster来实现。但是我表示说,文档很好。但是我不会用。我太菜。

最后某网友建议用日志来处理。我写了两天。终于是简单的凑出来了。

我用到了Serilog来写wal.因为不会写它的过滤条件。搞不来轮子。所以指定了 Fatal级别,也就是枚举里面为6的等级来记录到另一个目录下。

具体的配置如下。

//appsettings.json 中 

 1 "WriteTo": [
 2       //{ "Name": "Console" },
 3       {
 4         "Name": "File",
 5         "Args": {
 6           "path": "Log2s\\log.txt",
 7           "rollingInterval": "Day",
 8           "fileSizeLimitBytes": "10485760",
 9           "retainedFileCountLimit": 5,
10           "rollOnFileSizeLimit": true
11         }
12       },
13       {
14         "Name": "Logger",
15         "Args": {
16           "configureLogger": {
17             "Filter": [
18               {
19                 "Name": "ByIncludingOnly",
20                 "Args": {
21                   "expression": "@l = 'Fatal'"
22                 }
23               }
24             ],
25             "WriteTo": [
26               {
27                 "Name": "File",
28                 "Args": {
29                   "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] {Message:lj}{NewLine}",
30                   //"outputTemplate": "[{Timestamp:g} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}",
31                   "path": "wal\\wal.log",
32                   "rollingInterval": "Day",
33                   "fileSizeLimitBytes": "10485760",
34                   "retainedFileCountLimit": 200,
35                   "rollOnFileSizeLimit": true,
36                   "formatter": "Serilog.Formatting.Json.JsonFormatter",
37                   "flushToDiskInterval": "00:00:01"//刷盘时间
38                 }
39               }
40             ]
41           }
42         }
43       }
44     ]

 

代码调用

_logger?.LogCritical( "{@model}", model);

 

解析类:

 public class ReWALService : BackgroundService
    {
        public class ReWALDefine
        {    
            public long lastWriteTime { get; set; }//文件最后写入的时间,解析的时候
            public string logfile { get; set; }//文件名称
            public long streamPoint { get; set; }//在文件内容的位置
        }

        private readonly ILogger<ReWALService> _logger;
        
        private  string currentFile = "";
        private readonly DirectoryInfo _directoryInfo;
        public ReWALService(ILoggerFactory loggerFactory) 
        {
            this._logger = loggerFactory.CreateLogger<ReWALService>();
            var path = Path.Combine(Directory.GetCurrentDirectory(), "wal");
            _directoryInfo = new DirectoryInfo(path);
            //wallog
            currentFile = Path.Combine( path,"wallog.txt");
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                if (!File.Exists(currentFile))
                {
                    File.WriteAllText(currentFile, string.Empty);
                }

                while(!stoppingToken.IsCancellationRequested)
                {
                    var wallogContent = File.ReadAllText(currentFile);
                    ReWALDefine _walloginfo = fetchOrCreateDefine(wallogContent);
                    await Task.Delay(100);
                    DateTime oldDateTime = new DateTime(_walloginfo.lastWriteTime);
                    var walrecords = _directoryInfo.GetFiles("*.log").Where(q => q.LastWriteTime >= oldDateTime).Take(1).OrderBy(q => q.LastWriteTime);
                    var firstwalrecord = walrecords.FirstOrDefault(q => string.Compare(q.Name, currentFile, true) == 0);

                    foreach (var item in walrecords)
                    {   
                        _walloginfo.lastWriteTime = item.LastWriteTime.Ticks;
                        if (string.Compare(item.Name,_walloginfo.logfile,true) != 0)
                        {
                            _walloginfo.logfile = item.Name;
                            _walloginfo.streamPoint = 0;
                        }
                        _walloginfo.lastWriteTime += 1;

                        ReadFile(item.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), _walloginfo);
                        File.WriteAllText(currentFile, JsonConvert.SerializeObject(_walloginfo));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex.ToString());
            }
        }

        private static ReWALDefine fetchOrCreateDefine(string wallogContent)
        {
            ReWALDefine _walloginfo;
            if (string.IsNullOrWhiteSpace(wallogContent))
            {
                _walloginfo = new ReWALDefine();
            }
            else
            {
                _walloginfo = JsonConvert.DeserializeObject<ReWALDefine>(wallogContent);
            }

            return _walloginfo;
        }private void ReadFile(FileStream fs, ReWALDefine reWALDefine) {
            try
            {
                fs.Position = reWALDefine.streamPoint;
                using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8))
                {
                    while (sr.Peek() > -1)
                    {
                        Console.WriteLine("T:" + sr.ReadLine());//工作开始
                    }
                    Console.WriteLine(fs.Position);
                    Console.WriteLine(fs.Length);
                    reWALDefine.streamPoint = fs.Length;
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex.ToString());
            }
        }
    }

 

标签:wal,Name,记录,currentFile,庆军,walloginfo,ReWALDefine,string
From: https://www.cnblogs.com/forhell/p/17175596.html

相关文章

  • C# MongDb踩坑记录--分片写入报错 An upsert on a sharded collection must contain t
    环境C#mongdb.drivercollection分片_id为片键报错信息Awriteoperationresultedinanerror.WriteError:{Category:"Uncategorized",Code:61,Message:......
  • 记录用C#写折半查找算法实现
    折半查找算法前言最近要考试了,重新回顾一下之前学的算法,今天是折半查找,它的平均比较次数是Log2n思想给定一个有序数组A[0..n-1],和查找值K,返回K在A中的下标。折半查......
  • 安装Jupyter踩坑记录
    安装Anaconda之后,进行jupyter安装pipinstalljupyterlab报错解决pipisconfiguredwithlocationsthatrequireTLS/SSL,howeverthesslmoduleinPythonisnot......
  • docker记录
    安装(Ubuntu)1.更新aptapt-getupdate2.安装相关依赖apt-getinstallapt-transport-httpsca-certificatescurlgnupg-agen3.添加docker的官方密钥curl-fsSLhttp......
  • 网络问题定位工具记录
    网络问题定位工具记录老王内网发了一篇使用各种工具定位网络丢包问题的文章。里面的工具我发现都没用过。。。惭愧。。。赶紧补充linux命令知识。linux的nstat命令是做......
  • 记录一个移动端图片预览(支持旋转),使用css强制旋转的坑
    注:我并没有解决只是换了一个插件,记录一下问题1、需求要支持图片预览和旋转,<1>我选择是vant组件自带的ImagePreview图片预览,用的css强制实现旋转,<2>出现的问题是样......
  • linux 的防火墙 ufw、firwalld、iptables 、
    防火墙综述linux防火墙,常用的包括三种:ufw、firewalld和iptables。学习难度依次递增。ufw因为原生的iptable配置麻烦,学习成本较高。ufw全称UncomplicatedFirew......
  • 每日记录(十)2022.03.02
    今天学了很多,主要有网页的制作,但是不太熟练,明天继续    今天吃了铁板炒饭,味道还行,,我觉得那个鸡蛋还可以加点味道,不然不太好吃。晚上吃了麻辣烫,我发现学二食堂的麻......
  • 系统切换故障记录
    故障一:POS端查询当天报表时,出现GiftVoucherIssued故障时间:2023年3月1日11:43故障店铺:WinnieLui 故障原因:  故障二: ......
  • Pig 开发平台微服务接入 SkyWalking
    1、下载agentwgethttps://mirrors.tuna.tsinghua.edu.cn/apache/skywalking/java-agent/8.14.0/apache-skywalking-java-agent-8.14.0.tgz2、解压到微服务部署路径......