Jelajahi Sumber

1、修改保存知识块 返回结果 ,存什么 返回什么
2、增加日志记录

李思淳 5 tahun lalu
induk
melakukan
880d9de099

+ 348 - 0
TEAMModelOS.SDK/Helper/Common/LogHelper/LogHelper.cs

@@ -0,0 +1,348 @@
+using log4net;
+using log4net.Config;
+using log4net.Repository;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Helper.Common.LogHelper
+{
+    public class LogHelper
+    {
+        private static ILoggerRepository repository;
+
+        static LogHelper()
+        {
+            //log4net
+            ILoggerRepository _repository = LogManager.CreateRepository("NETCoreRepository");
+            //指定配置文件
+            XmlConfigurator.Configure(_repository, new FileInfo("log4net.config"));
+            repository = _repository;
+
+        }
+
+        private static readonly ConcurrentDictionary<Type, ILog> Loggers = new ConcurrentDictionary<Type, ILog>();
+
+        public static string GetLogContent(string logkey)
+        {
+            logkey = "【" + logkey + "】";
+            string logfile = System.DateTime.Now.ToString("yyyyMMdd");
+            string path = Directory.GetCurrentDirectory() + "/logfile/" + logfile + ".log";
+
+            //获取正在占用的文件
+            FileStream fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+            StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
+            String line;
+            StringBuilder builder = new StringBuilder();
+            while ((line = sr.ReadLine()) != null)
+            {
+                builder.Append(line.ToString());
+            }
+            sr.Close();
+            string log = builder.ToString();
+            string s = GetBetweenStr(log, logkey, logkey);
+            return s;
+        }
+
+        /// <summary>
+        /// 获取记录器
+        /// </summary>
+        /// <param name="source">soruce</param>
+        /// <returns></returns>
+        private static ILog GetLogger(Type source)
+        {
+            if (Loggers.ContainsKey(source))
+            {
+                return Loggers[source];
+            }
+            else
+            {
+
+                ILog logger = LogManager.GetLogger(repository.Name, source);
+                Loggers.TryAdd(source, logger);
+                return logger;
+            }
+        }
+
+        /* Log a message object */
+
+        /// <summary>
+        /// 调试信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Debug(object source, string message)
+        {
+            Debug(source.GetType(), message);
+        }
+
+        /// <summary>
+        /// 调试信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="ps">ps</param>
+        public static void Debug(object source, string message, params object[] ps)
+        {
+            Debug(source.GetType(), string.Format(message, ps));
+        }
+
+        /// <summary>
+        /// 调试信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Debug(Type source, string message)
+        {
+            ILog logger = GetLogger(source);
+            if (logger.IsDebugEnabled)
+            {
+                logger.Debug(message);
+            }
+        }
+
+        /// <summary>
+        /// 关键信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Info(object source, object message)
+        {
+            Info(source.GetType(), message);
+        }
+
+        /// <summary>
+        /// 关键信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Info(Type source, object message)
+        {
+            ILog logger = GetLogger(source);
+            if (logger.IsInfoEnabled)
+            {
+                logger.Info(message);
+            }
+        }
+
+        /// <summary>
+        /// 警告信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Warn(object source, object message)
+        {
+            Warn(source.GetType(), message);
+        }
+
+        /// <summary>
+        /// 警告信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Warn(Type source, object message)
+        {
+            ILog logger = GetLogger(source);
+            if (logger.IsWarnEnabled)
+            {
+                logger.Warn(message);
+            }
+        }
+
+        /// <summary>
+        /// 错误信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Error(object source, object message)
+        {
+            Error(source.GetType(), message);
+        }
+
+        /// <summary>
+        /// 错误信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Error(Type source, object message)
+        {
+            ILog logger = GetLogger(source);
+            if (logger.IsErrorEnabled)
+            {
+                logger.Error(message);
+            }
+        }
+
+        /// <summary>
+        /// 失败信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Fatal(object source, object message)
+        {
+            Fatal(source.GetType(), message);
+        }
+
+        /// <summary>
+        /// 失败信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        public static void Fatal(Type source, object message)
+        {
+            ILog logger = GetLogger(source);
+            if (logger.IsFatalEnabled)
+            {
+                logger.Fatal(message);
+            }
+        }
+
+        /* Log a message object and exception */
+
+        /// <summary>
+        /// 调试信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Debug(object source, object message, Exception exception)
+        {
+            Debug(source.GetType(), message, exception);
+        }
+
+        /// <summary>
+        /// 调试信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Debug(Type source, object message, Exception exception)
+        {
+            GetLogger(source).Debug(message, exception);
+        }
+
+        /// <summary>
+        /// 关键信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Info(object source, object message, Exception exception)
+        {
+            Info(source.GetType(), message, exception);
+        }
+
+        /// <summary>
+        /// 关键信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Info(Type source, object message, Exception exception)
+        {
+            GetLogger(source).Info(message, exception);
+        }
+
+        /// <summary>
+        /// 警告信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Warn(object source, object message, Exception exception)
+        {
+            Warn(source.GetType(), message, exception);
+        }
+
+        /// <summary>
+        /// 警告信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Warn(Type source, object message, Exception exception)
+        {
+            GetLogger(source).Warn(message, exception);
+        }
+
+        /// <summary>
+        /// 错误信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Error(object source, object message, Exception exception)
+        {
+            Error(source.GetType(), message, exception);
+        }
+
+        /// <summary>
+        /// 错误信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Error(Type source, object message, Exception exception)
+        {
+            GetLogger(source).Error(message, exception);
+        }
+
+        /// <summary>
+        /// 失败信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Fatal(object source, object message, Exception exception)
+        {
+            Fatal(source.GetType(), message, exception);
+        }
+
+        /// <summary>
+        /// 失败信息
+        /// </summary>
+        /// <param name="source">source</param>
+        /// <param name="message">message</param>
+        /// <param name="exception">ex</param>
+        public static void Fatal(Type source, object message, Exception exception)
+        {
+            GetLogger(source).Fatal(message, exception);
+        }
+
+        /*        public static void DingDingAndLog(object source, string loginfo, Teacher loginTeacher, string bizcode, string targetTeamModelId, string solvedStatus)
+                {
+                    string uuidKey =  Guid.NewGuid().ToString();
+                    string logkey = "【" + uuidKey + "】";
+                    StringBuilder message = new StringBuilder("请查看日志文件访问链接:" + "【api/teacher/loginfo?logfile=" + DateTime.Now.ToString("yyyyMMdd") + "&logkey=" + uuidKey + "】。\n");
+                    DingDingWebhook.sendWebhook("醍摩豆杯报名网站", bizcode, solvedStatus, message.ToString(), loginTeacher.phone, "【" + loginTeacher.teamModelId + "-" + loginTeacher.userName + "-" +
+                        loginTeacher.phone + "】", "【" + targetTeamModelId + "】");
+                    LogHelper.Info(source, logkey + loginfo + logkey);
+                }*/
+
+
+        /// <summary>
+        /// 获取两个字符串中间的字符串
+        /// </summary>
+        /// <param name="str">要处理的字符串,例ABCD</param>
+        /// <param name="str1">第1个字符串,例AB</param>
+        /// <param name="str2">第2个字符串,例D</param>
+        /// <returns>例返回C</returns>
+        public static string GetBetweenStr(string str, string str1, string str2)
+        {
+            int i1 = str.IndexOf(str1);
+            if (i1 < 0) //找不到返回空
+            {
+                return "";
+            }
+            int i2 = str.LastIndexOf(str2); //从找到的第1个字符串后再去找
+            if (i2 < 0) //找不到返回空
+            {
+                return "";
+            }
+            return str.Substring(i1 + str1.Length, i2 - i1 - str1.Length);
+        }
+
+    }
+}

+ 1 - 0
TEAMModelOS.SDK/TEAMModelOS.SDK.csproj

@@ -11,6 +11,7 @@
     <PackageReference Include="HtmlAgilityPack" Version="1.11.16" />
     <PackageReference Include="HtmlAgilityPack" Version="1.11.16" />
     <PackageReference Include="IdentityModel" Version="4.0.0" />
     <PackageReference Include="IdentityModel" Version="4.0.0" />
     <PackageReference Include="LiteDB" Version="4.1.4" />
     <PackageReference Include="LiteDB" Version="4.1.4" />
+    <PackageReference Include="log4net" Version="2.0.8" />
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
     <PackageReference Include="Microsoft.AspNetCore.Authorization" Version="3.1.0" />
     <PackageReference Include="Microsoft.AspNetCore.Authorization" Version="3.1.0" />
     <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
     <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />

+ 1 - 1
TEAMModelOS/Controllers/Syllabus/KnowledgeController.cs

@@ -289,7 +289,7 @@ namespace TEAMModelOS.Controllers.Syllabus
                 }
                 }
                 if (resourceReferences.Count > 0) await _table.SaveOrUpdateAll(resourceReferences);
                 if (resourceReferences.Count > 0) await _table.SaveOrUpdateAll(resourceReferences);
                 List<SchoolBlock> ts = await _cosmos.SaveAll(schoolBlocks);
                 List<SchoolBlock> ts = await _cosmos.SaveAll(schoolBlocks);
-                if (ts.Count > 0) builder.Data("保存或新增成功");
+                if (ts.Count > 0) builder.Data(ts).Extend(new Dictionary<string, object> { { "count", ts.Count } });
                 else builder.Error(false, ResponseCode.FAILED, "失败");
                 else builder.Error(false, ResponseCode.FAILED, "失败");
             }
             }
             return builder.build();
             return builder.build();

+ 14 - 1
TEAMModelOS/Controllers/Syllabus/ResourceController.cs

@@ -1,10 +1,13 @@
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc;
+using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
 using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
 using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
 using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
 using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
+using TEAMModelOS.SDK.Helper.Common.JsonHelper;
+using TEAMModelOS.SDK.Helper.Common.LogHelper;
 using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;
 using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;
 using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
 using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
 using TEAMModelOS.Service.Models.Core;
 using TEAMModelOS.Service.Models.Core;
@@ -107,7 +110,17 @@ namespace TEAMModelOS.Controllers.Syllabus
             List<T> schoolBlocks = await _cosmos.FindByParams<T>(request.@params);
             List<T> schoolBlocks = await _cosmos.FindByParams<T>(request.@params);
             if (schoolBlocks.IsNotEmpty())
             if (schoolBlocks.IsNotEmpty())
             {
             {
-                schoolBlocks.ForEach(x => { _cosmos.DeleteAsync<T>(x); });
+                schoolBlocks.ForEach(x => {
+                    //log4net 日志記錄
+                    string uuidKey = Guid.NewGuid().ToString();
+                    string logkey = "【" + uuidKey + "】";
+                    LogHelper.Info(this,
+                                   logkey 
+                                   + "删除内容" 
+                                   + x.ToJson() 
+                                   + logkey);
+                    _cosmos.DeleteAsync<T>(x); 
+                });
                 builder.Data("删除成功");
                 builder.Data("删除成功");
             }
             }
             else
             else

+ 35 - 0
TEAMModelOS/log4net.config

@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+  <!-- This section contains the log4net configuration settings -->
+  <log4net>
+    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
+      <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
+    </appender>
+    <appender name="FileAppender" type="log4net.Appender.FileAppender">
+      <!--<file value="log-file.log" />-->
+      <appendToFile value="true" />
+      <layout type="log4net.Layout.PatternLayout">
+        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
+      </layout>
+    </appender>
+    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
+      <file value="logfile/" />
+      <appendToFile value="true" />
+      <rollingStyle value="Composite" />
+      <staticLogFileName value="false" />
+      <datePattern value="yyyyMMdd'.log'" />
+      <maxSizeRollBackups value="10" />
+      <maximumFileSize value="1MB" />
+      <layout type="log4net.Layout.PatternLayout">
+        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
+      </layout>
+    </appender>
+    <!-- Setup the root category, add the appenders and set the default level -->
+    <root>
+      <level value="ALL" />
+      <appender-ref ref="ConsoleAppender" />
+      <appender-ref ref="FileAppender" />
+      <appender-ref ref="RollingLogFileAppender" />
+    </root>
+  </log4net>
+</configuration>