|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|