lcl 7 месяцев назад
Родитель
Сommit
54beaa3afc
5 измененных файлов с 291 добавлено и 38 удалено
  1. BIN
      .DS_Store
  2. 71 0
      AppStart/SLS/LogHelper.cs
  3. 168 0
      AppStart/SLS/SLS.cs
  4. 13 0
      Controllers/HomeController.cs
  5. 39 38
      MySystem.csproj

+ 71 - 0
AppStart/SLS/LogHelper.cs

@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading;
+using Library;
+using LitJson;
+using MySystem;
+
+public class LogHelper
+{
+    public readonly static LogHelper Instance = new LogHelper();
+    private LogHelper()
+    { }
+
+    public void WriteLog(string Content, string Topic)
+    {
+        Dictionary<string, string> dic = new Dictionary<string, string>();
+        dic.Add("Topic", Topic);
+        dic.Add("Content", Content);
+        RedisDbconn.Instance.AddList("SlsLogQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic));
+    }
+
+    public void Start()
+    {
+        Thread th = new Thread(DoWorks);
+        th.IsBackground = true;
+        th.Start();
+    }
+
+    public void DoWorks()
+    {
+        while (true)
+        {
+            string content = RedisDbconn.Instance.RPop<string>("SlsLogQueue");
+            if (!string.IsNullOrEmpty(content))
+            {
+                try
+                {
+                    DoQueue(content);
+                }
+                catch (Exception ex)
+                {
+                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "SLS日志异常");
+                }
+            }
+            else
+            {
+                Thread.Sleep(5000);
+            }
+        }
+    }
+
+    public void DoQueue(string content)
+    { 
+        JsonData JsonObj = JsonMapper.ToObject(content);
+        string Topic = JsonObj["Topic"].ToString();
+        string Cont = JsonObj["Content"].ToString();
+        string Tags = JsonObj["Tags"].ToString();
+        Dictionary<string, string> otherTags = new Dictionary<string, string>();
+        if(!string.IsNullOrEmpty(Tags))
+        {
+            string[] TagList = Tags.Split(',');
+            JsonData contObj = JsonMapper.ToObject(Cont);
+            foreach(string Tag in TagList)
+            {
+                otherTags.Add(key: Tag, contObj[Tag].ToString());
+            }
+        }
+        SLS.WriteLog(DateTime.Now, Topic, Cont, otherTags);
+    }
+}

+ 168 - 0
AppStart/SLS/SLS.cs

@@ -0,0 +1,168 @@
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+using Aliyun.Api.LogService;
+using Aliyun.Api.LogService.Domain.Log;
+using Aliyun.Api.LogService.Domain.LogStore.Index;
+using Aliyun.Api.LogService.Infrastructure.Protocol;
+
+namespace MySystem
+{
+    public class SLS
+    {
+        //配置AccessKey、服务入口、Project名称、Logstore名称等相关信息。
+        //日志服务的服务入口。更多信息,请参见服务入口。
+        //此处以杭州为例,其它地域请根据实际情况填写。
+        private static string endpoint = "cn-chengdu.log.aliyuncs.com";
+        //阿里云访问密钥AccessKey。更多信息,请参见访问密钥。阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
+        private static string accessKeyId = "LTAI5t9L1WAyzPaQzX57kSxG";
+        private static string accessKeySecret = "BqdskHlgU402kHhlpvdTOLsQZ63xKw";
+        //Project名称。
+        private static string project = "kexiaoshuang";
+        //Logstore名称。
+        private static string logstore = "aliyun-test-logstore";
+        //创建日志服务Client。
+        private static ILogServiceClient client = BuildSimpleClient();
+
+        static async Task Main()
+        {
+            //创建Project。
+            var proRes = await client.CreateProjectAsync(project, "des");
+            check(proRes);
+            Console.WriteLine("Create project success");
+            Thread.Sleep(120 * 1000);
+            
+            //创建Logstore。
+            var storeRes = await client.CreateLogStoreAsync(logstore, 3, 2);
+            check(storeRes);
+            Console.WriteLine("Create logstore success");
+            Thread.Sleep(10 * 1000);
+            
+            //为Logstore创建索引。
+            var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] {' ', ','}));
+            check(indRes);
+            Console.WriteLine("Create Index success");
+            Thread.Sleep(60 * 1000);
+
+            //向Logstore写入数据。
+            // await PostLogs();
+            Console.WriteLine("Post logs success");
+            Thread.Sleep(3000);
+            //查询日志。
+            await GetLogs();
+        }
+
+        public static async void CreateProject(string name, string detail)
+        {
+            var proRes = await client.CreateProjectAsync(name, detail);
+            check(proRes);
+        }
+
+        public static async void CreateLogStore(string logstore)
+        {
+            var storeRes = await client.CreateLogStoreAsync(logstore, 3, 2, project);
+            check(storeRes);
+        }
+
+        public static async void CreateIndex(string logstore)
+        {
+            var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] {' ', ','}), project);
+            check(indRes);
+        }
+
+        public static async Task GetLogs()
+        {
+            var logsRes = await client.GetLogsAsync(logstore, DateTimeOffset.UtcNow.AddMinutes(-1),
+                DateTimeOffset.UtcNow,
+                "test", "", 100, 0);
+            check(logsRes);
+            foreach (var log in logsRes.Result.Logs)
+            {
+                foreach (var key in log.Keys)
+                {
+                    log.TryGetValue(key, out var value);
+                    Console.WriteLine(key + " : " + value);
+                }
+
+                Console.WriteLine("======");
+            }
+        }
+
+        public static async Task<IList<IDictionary<string, string>>> GetLogs(string logstore, string topic, string query, DateTimeOffset from, int offset = 0, int line = 50)
+        {
+            var logsRes = await client.GetLogsAsync(logstore, from,
+                DateTimeOffset.UtcNow,
+                topic, query, line, offset);
+            check(logsRes);
+            return logsRes.Result.Logs;
+        }
+
+        public static async void PostLogs(string logstore, string Topic, string Source, Dictionary<String, String> Contents, Dictionary<String, String> LogTags)
+        {
+            var response = await client.PostLogStoreLogsAsync(logstore, new LogGroupInfo
+            {
+                Topic = Topic,
+                Source = Source,
+                LogTags = LogTags,
+                Logs = new List<LogInfo>
+                {
+                    new LogInfo
+                    {
+                        Time = DateTimeOffset.Now,
+                        Contents = Contents
+                        // new Dictionary<String, String>
+                        // {
+                        //     {"name", "zs"},
+                        //     {"age", "18"},
+                        //     {"address", String.Empty}
+                        // }
+                    }
+                }
+            });
+            check(response);
+        }
+
+        public static ILogServiceClient BuildSimpleClient()
+            => LogServiceClientBuilders.HttpBuilder
+                .Endpoint(endpoint, project)
+                .Credential(accessKeyId, accessKeySecret)
+                .Build();
+
+        public static void check(IResponse res)
+        {
+            if (!res.IsSuccess)
+            {
+                throw new ApplicationException(res.Error.ErrorMessage);
+            }
+        }
+
+
+
+
+        /// <summary>
+        /// SLS日志基础方法
+        /// </summary>
+        /// <param name="createDate"></param>
+        /// <param name="topic"></param>
+        /// <param name="content"></param>
+        /// <param name="otherTags"></param>
+        /// <returns></returns>
+        public static string WriteLog(DateTime createDate, string topic, string content, Dictionary<string, string> otherTags)
+        {
+            Dictionary<string, string> tags = new Dictionary<string, string>();
+            tags.Add("datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
+            tags.Add("date", DateTime.Now.ToString("yyyy-MM-dd"));
+            tags.Add("month", DateTime.Now.ToString("yyyy-MM"));
+            tags.Add("year", DateTime.Now.ToString("yyyy"));
+            foreach(string key in otherTags.Keys)
+            {
+                tags.Add(key, otherTags[key]);
+            }
+            SLS.PostLogs("spserver", topic, "47.108.229.115", new Dictionary<string, string>{
+                {"content", content}
+            }, tags);
+            return "ok";
+        }
+    }
+}

+ 13 - 0
Controllers/HomeController.cs

@@ -5,6 +5,8 @@ using MySystem.MainModels;
 using System.Linq;
 using Library;
 using LitJson;
+using System.Threading.Tasks;
+using System;
 
 namespace MySystem.Controllers
 {
@@ -64,5 +66,16 @@ namespace MySystem.Controllers
             }
             return "ok";
         }
+
+        public async Task<IList<IDictionary<string, string>>> test1(string condi)
+        {
+            string[] condiList = condi.Split('|');
+            string logstore = "spserver";
+            string topic = condiList[0];
+            string keyword = condiList[1];
+            string month = condiList[2];
+            IList<IDictionary<string, string>> logs = await SLS.GetLogs(logstore, topic, "* and content: " + keyword, DateTimeOffset.Now.AddMonths(-int.Parse(month)));
+            return logs;
+        }
     }
 }

+ 39 - 38
MySystem.csproj

@@ -1,33 +1,33 @@
-<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<?xml version="1.0" encoding="utf-8" standalone="yes"?>
 <Project Sdk="Microsoft.NET.Sdk.Web">
   <PropertyGroup>
     <TargetFramework>netcoreapp3.0</TargetFramework>
   </PropertyGroup>
   <ItemGroup>
-    <DotNetCliToolReference Include="dotnet-svcutil" Version="1.0.*"/>
+    <DotNetCliToolReference Include="dotnet-svcutil" Version="1.0.*" />
   </ItemGroup>
   <ItemGroup>
-    <Folder Include="Areas\"/>
-    <Folder Include="Areas\Admin\"/>
-    <Folder Include="Areas\Api\"/>
-    <Folder Include="Areas\Web\"/>
-    <Folder Include="Areas\Admin\Controllers\"/>
-    <Folder Include="Areas\Admin\Views\"/>
-    <Folder Include="Areas\Web\Controllers\"/>
-    <Folder Include="Areas\Web\Views\"/>
-    <Folder Include="Areas\Api\Controllers\"/>
-    <Folder Include="PublicClass\"/>
-    <Folder Include="AppStart\"/>
-    <Folder Include="Views\Home\"/>
-    <Folder Include="Areas\Admin\Views\Home\"/>
-    <Folder Include="Areas\Web\Views\Article\"/>
-    <Folder Include="PublicClass\GraphQL\"/>
-    <Folder Include="PublicClass\GraphQL\Types\"/>
-    <Folder Include="Areas\Web\Views\Page\"/>
-    <Folder Include="Areas\Web\Views\Pay\"/>
-    <Folder Include="AppStart\Helper\"/>
-    <Folder Include="Areas\Api\Controllers\activity\"/>
-    <Folder Include="AppStart\Activity\"/>
+    <Folder Include="Areas\" />
+    <Folder Include="Areas\Admin\" />
+    <Folder Include="Areas\Api\" />
+    <Folder Include="Areas\Web\" />
+    <Folder Include="Areas\Admin\Controllers\" />
+    <Folder Include="Areas\Admin\Views\" />
+    <Folder Include="Areas\Web\Controllers\" />
+    <Folder Include="Areas\Web\Views\" />
+    <Folder Include="Areas\Api\Controllers\" />
+    <Folder Include="PublicClass\" />
+    <Folder Include="AppStart\" />
+    <Folder Include="Views\Home\" />
+    <Folder Include="Areas\Admin\Views\Home\" />
+    <Folder Include="Areas\Web\Views\Article\" />
+    <Folder Include="PublicClass\GraphQL\" />
+    <Folder Include="PublicClass\GraphQL\Types\" />
+    <Folder Include="Areas\Web\Views\Page\" />
+    <Folder Include="Areas\Web\Views\Pay\" />
+    <Folder Include="AppStart\Helper\" />
+    <Folder Include="Areas\Api\Controllers\activity\" />
+    <Folder Include="AppStart\Activity\" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="aliyun-net-sdk-core">
@@ -50,26 +50,27 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
-    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0"/>
-    <PackageReference Include="RabbitMQ.Client" Version="6.2.2"/>
-    <PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*"/>
-    <PackageReference Include="System.ServiceModel.Http" Version="4.4.*"/>
-    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*"/>
-    <PackageReference Include="System.ServiceModel.Security" Version="4.4.*"/>
-    <PackageReference Include="ZKWeb.System.Drawing" Version="4.0.1"/>
-    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.0.1"/>
-    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0"/>
-    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0"/>
+    <PackageReference Include="Aliyun.Api.LogService" Version="1.1.0" />
+    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
+    <PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
+    <PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
+    <PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
+    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
+    <PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />
+    <PackageReference Include="ZKWeb.System.Drawing" Version="4.0.1" />
+    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.0.1" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
+    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
       <PrivateAssets>all</PrivateAssets>
     </PackageReference>
-    <PackageReference Include="GraphQL" Version="2.4.0"/>
-    <PackageReference Include="System.Drawing.Common" Version="4.7.0"/>
-    <PackageReference Include="MySql.Data" Version="8.0.18"/>
-    <PackageReference Include="CSRedisCore" Version="3.6.5"/>
+    <PackageReference Include="GraphQL" Version="2.4.0" />
+    <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
+    <PackageReference Include="MySql.Data" Version="8.0.18" />
+    <PackageReference Include="CSRedisCore" Version="3.6.5" />
   </ItemGroup>
   <ItemGroup>
-    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3"/>
+    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
   </ItemGroup>
 </Project>