123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- 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);
- }
- }
- }
|