.net core 使用阿里云分布式日志的配置方法
前言
好久沒(méi)有出來(lái)夸白了,今天教大家簡(jiǎn)單的使用阿里云分布式日志,來(lái)存儲(chǔ)日志,沒(méi)有阿里云賬號(hào)的,可以免費(fèi)注冊(cè)一個(gè)
開(kāi)通阿里云分布式日志(有一定的免費(fèi)額度,個(gè)人測(cè)試學(xué)習(xí)完全沒(méi)問(wèn)題的,香)
阿里云日志地址:https://sls.console.aliyun.com/lognext/profile
先開(kāi)通阿里云日志,這個(gè)比較簡(jiǎn)單授權(quán)就可以了
選擇接入數(shù)據(jù),我們這里選 .net
選擇項(xiàng)目名稱,沒(méi)有項(xiàng)目的可以去創(chuàng)建一個(gè),項(xiàng)目名稱后面會(huì)用到,如果你有購(gòu)買阿里云ecs,項(xiàng)目區(qū)域最好選擇跟ecs同一個(gè)區(qū)域(每個(gè)區(qū)域的地址不一樣,同一個(gè)區(qū)域可以選擇內(nèi)網(wǎng)通訊,速度更快),如果沒(méi)有,就隨便選個(gè)區(qū)域,我這里選擇的是杭州
選擇日志庫(kù),沒(méi)有就創(chuàng)建一個(gè)
數(shù)據(jù)源配置,這個(gè)先不用管,后面有教程
設(shè)置分析配置,例如我這里設(shè)置了兩個(gè),可以根據(jù)業(yè)務(wù)需求來(lái),沒(méi)有特殊要求不用設(shè)置
開(kāi)通完成,可以正??吹絻x盤表
設(shè)置密鑰
通過(guò)sdk 寫入日志
阿里云有提供對(duì)應(yīng)的sdk(阿里云 .net sdk的質(zhì)量大家都懂),它主要是通過(guò)構(gòu)造一個(gè)靜態(tài)對(duì)象來(lái)提供訪問(wèn)的,地址: https://github.com/aliyun/aliyun-log-dotnetcore-sdk
阿里云代碼
logserviceclientbuilders.httpbuilder .endpoint("<endpoint>", "<projectname>") .credential("<accesskeyid>", "<accesskey>") .build();
阿里云提供的依賴注入代碼(autofac),很遺憾按照這個(gè)方式,并沒(méi)有獲取到對(duì)象
using aliyun.api.logservice; using autofac; namespace examples.dependencyinjection { public static class autofacexample { public static void register(containerbuilder containerbuilder) { containerbuilder .register(context => logserviceclientbuilders.httpbuilder // 服務(wù)入口<endpoint>及項(xiàng)目名<projectname> .endpoint("<endpoint>", "<projectname>") // 訪問(wèn)密鑰信息 .credential("<accesskeyid>", "<accesskey>") .build()) // `ilogserviceclient`所有成員是線程安全的,建議使用singleton模式。 .singleinstance(); } } }
中間個(gè)有小插曲,由于公司使用阿里云日志比較早,也非常穩(wěn)定,替換成我申請(qǐng)的阿里云日志的配置,發(fā)送日志一直報(bào)錯(cuò),找了半天沒(méi)找到原因,提了工單,原來(lái)阿里云使用了新的sdk
重新封裝阿里云日志sdk(aliyun.log.core) https://github.com/wmowm/aliyun.log.core問(wèn)了下群友,剛好有大佬重寫過(guò)向阿里云提交日志這塊,一番py交易,代碼塊到手,主要是數(shù)據(jù)組裝,加密,發(fā)送,發(fā)送部分的代碼基于http的protobuf服務(wù)實(shí)現(xiàn),這里直接從阿里云開(kāi)源的sdk里拷貝過(guò)來(lái),開(kāi)始重新封裝,主要實(shí)現(xiàn)以下功能
- 實(shí)現(xiàn).net core di
- 加入隊(duì)列,讓日志先入列,再提交到阿里云,提高系統(tǒng)吞吐量
- 對(duì)日志模型進(jìn)行封裝,滿足基礎(chǔ)業(yè)務(wù)需求
代碼如下
添加servicecollection 拓展,定義一個(gè)阿里云日志的配置信息委托,然后將需要注入的服務(wù)注冊(cè)進(jìn)去即可
public static aliyunlogbuilder addaliyunlog(this iservicecollection services, action<aliyunslsoptions> setupaction) { if (setupaction == null) { throw new argumentnullexception(nameof(setupaction)); } //var options = new aliyunslsoptions(); //setupaction(options); services.configure(setupaction); services.addhttpclient(); services.addsingleton<aliyunslsclient>(); services.addsingleton<aliyunlogclient>(); services.addhostedservice<hostedservice>(); return new aliyunlogbuilder(services); }
加入隊(duì)列比較簡(jiǎn)單,定義一個(gè)隊(duì)列,使用hostedservice 消費(fèi)隊(duì)列
/// <summary> /// 寫日志 /// </summary> /// <param name="log"></param> /// <returns></returns> public async task log(logmodel log) { aliyunlogbuilder.logqueue.enqueue(log); }
/// <summary> /// 消費(fèi)隊(duì)列 /// </summary> task.run(async () => { using (var servicescope = _provider.getservice<iservicescopefactory>().createscope()) { var _options = servicescope.serviceprovider.getrequiredservice<ioptions<aliyunslsoptions>>(); var _client = servicescope.serviceprovider.getrequiredservice<aliyunslsclient>(); while (true) { try { if (aliyunlogbuilder.logqueue.count>0) { var log = aliyunlogbuilder.logqueue.dequeue(); var loginfo = new loginfo { contents = { {"topic", log.topic.tostring()}, {"orderno", log.orderno}, {"classname", log.classname}, { "desc",log.desc}, { "html",log.html}, { "postdate",log.postdate}, }, time = datetime.parse(log.postdate) }; list<loginfo> list = new list<loginfo>() { loginfo }; var loggroupinfo = new loggroupinfo { topic = log.topic.tostring(), source = "localhost", logs = list }; await _client.postlogs(new postlogsrequest(_options.value.logstorename, loggroupinfo)); } else { await task.delay(1000); } } catch (exception ex) { await task.delay(1000); } } } });
定義日志模型,可以根據(jù)業(yè)務(wù)情況拓展
public class logmodel { /// <summary> /// 所在類 /// </summary> public string classname { get; set; } /// <summary> /// 訂單號(hào) /// </summary> public string orderno { get; set; } /// <summary> /// 提交時(shí)間 /// </summary> public string postdate { get; set; } /// <summary> /// 描述 /// </summary> public string desc { get; set; } /// <summary> /// 長(zhǎng)字段描述 /// </summary> public string html { get; set; } /// <summary> /// 日志主題 /// </summary> public string topic { get; set; } = "3"; }
使用aliyun.log.core
獲取aliyun.log.core包
方案a. install-package aliyun.log.core
方案b. nuget包管理工具搜索 aliyun.log.core
添加中間件
services.addaliyunlog(m => { m.accesskey = sls.getvalue<string>("accesskey"); m.accesskeyid = sls.getvalue<string>("accesskeyid"); m.endpoint = sls.getvalue<string>("host"); m.project = sls.getvalue<string>("project"); m.logstorename = sls.getvalue<string>("logstorename"); });
寫入日志
//獲取對(duì)象 private readonly ioptions<slsoptions> _options; private readonly aliyunlogclient _aliyunlogclient; public homecontroller(ioptions<slsoptions> options, aliyunlogclient aliyunlogclient) { _options = options; _aliyunlogclient = aliyunlogclient; } [httpget("/api/sendlog")] public async task<jsonresult> sendlog(string topic="1") { //日志模型 logmodel logmodel = new logmodel() { classname = "aliyun.log", desc = "6666666666xxxxxx", html = "99999999999xxxxx", topic = topic, orderno = guid.newguid().tostring("n"), postdate = datetime.now.tostring("yyyy-mm-dd hh:mm:ss") }; await _aliyunlogclient.log(logmodel); return json("0"); }
簡(jiǎn)單的查詢?nèi)罩?/h2>
同事寫了一個(gè)查詢阿里云日志的工具,非常實(shí)用,我用 .net core又抄了一遍,一并開(kāi)源在github,地址: https://github.com/wmowm/aliyun.log.core/tree/main/aliyun.log.core.client
推薦我自己寫的一個(gè)redis消息隊(duì)列中間件initq,操作簡(jiǎn)單可以下載的玩玩
https://github.com/wmowm/initq
關(guān)于.net core 使用阿里云分布式日志的文章就介紹至此,更多相關(guān).net core分布式日志內(nèi)容請(qǐng)搜索碩編程以前的文章,希望大家多多支持碩編程!