系统新增调用api接口记录至db

master
xiaochanghai 12 months ago
parent fcd6383fae
commit e19d0a5284
  1. 4
      Tiobon.Core.Common/DB/DbSql/DbInsert.cs
  2. 15
      Tiobon.Core.Common/Helper/FileHelper.cs
  3. 6
      Tiobon.Core.Common/LogHelper/LogContextStatic.cs
  4. 43
      Tiobon.Core.Common/LogHelper/LogLock.cs
  5. 12
      Tiobon.Core.Extensions/Middlewares/RecordAccessLogsMiddleware.cs
  6. 14
      Tiobon.Core.Model/ViewModels/UserAccessModel.cs

@ -270,11 +270,11 @@ namespace Tiobon.Core.Common
//create_by //create_by
var createBy = UserContext.Current.User_Id; var createBy = UserContext.Current.User_Id;
Values("CreatedBy", createBy); Values("CreateBy", createBy);
//create_date //create_date
DateTime createDate = DateTime.Now; DateTime createDate = DateTime.Now;
Values("CreatedTime", createDate); Values("CreateTime", createDate);
//create_program //create_program
//Values("CREATED_PROGRAM", createProgram); //Values("CREATED_PROGRAM", createProgram);

@ -73,15 +73,14 @@ namespace Tiobon.Core.Common.Helper
/// <returns>可用文件名</returns> /// <returns>可用文件名</returns>
public static string GetAvailableFileWithPrefixOrderSize(string folderPath, string prefix, int size = 1 * 1024 * 1024, string ext = ".log") public static string GetAvailableFileWithPrefixOrderSize(string folderPath, string prefix, int size = 1 * 1024 * 1024, string ext = ".log")
{ {
var allFiles = new DirectoryInfo(folderPath); //var allFiles = new DirectoryInfo(folderPath);
var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList(); //var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList();
if (selectFiles.Count > 0)
{
return selectFiles.FirstOrDefault().FullName;
}
return Path.Combine(folderPath, $@"{prefix}_{DateTime.Now.DateToTimeStamp()}.log"); //if (selectFiles.Count > 0)
//{
// return selectFiles.FirstOrDefault().FullName;
//}
return Path.Combine(folderPath, $@"{prefix}.log");
} }
public static string GetAvailableFileNameWithPrefixOrderSize(string _contentRoot, string prefix, int size = 1 * 1024 * 1024, string ext = ".log") public static string GetAvailableFileNameWithPrefixOrderSize(string _contentRoot, string prefix, int size = 1 * 1024 * 1024, string ext = ".log")
{ {

@ -1,6 +1,4 @@
using System.IO; namespace Tiobon.Core.Common.LogHelper;
namespace Tiobon.Core.Common.LogHelper;
public class LogContextStatic public class LogContextStatic
{ {
@ -13,7 +11,7 @@ public class LogContextStatic
} }
public static readonly string BaseLogs = "Logs"; public static readonly string BaseLogs = "Logs";
public static readonly string BasePathLogs = @"Logs"; public static string BasePathLogs { get { return DateTime.Now.ToString("yyyyMMdd"); } }
public static readonly string LogSource = "LogSource"; public static readonly string LogSource = "LogSource";
public static readonly string AopSql = "AopSql"; public static readonly string AopSql = "AopSql";

@ -1,17 +1,14 @@
using Tiobon.Core.Common.Helper; using Newtonsoft.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using Serilog; using Serilog;
using Tiobon.Core.Common.Helper;
using Tiobon.Core.Model;
using Tiobon.Core.Common.DB.Dapper;
namespace Tiobon.Core.Common.LogHelper namespace Tiobon.Core.Common.LogHelper;
public class LogLock
{ {
public class LogLock
{
static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim(); static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
static int WritedCount = 0; static int WritedCount = 0;
static int FailedCount = 0; static int FailedCount = 0;
@ -86,7 +83,7 @@ namespace Tiobon.Core.Common.LogHelper
// 因进入与退出写入模式应在同一个try finally语句块内,所以在请求进入写入模式之前不能触发异常,否则释放次数大于请求次数将会触发异常 // 因进入与退出写入模式应在同一个try finally语句块内,所以在请求进入写入模式之前不能触发异常,否则释放次数大于请求次数将会触发异常
LogWriteLock.EnterWriteLock(); LogWriteLock.EnterWriteLock();
var folderPath = Path.Combine(_contentRoot, "Log"); var folderPath = Path.Combine(_contentRoot, $@"Logs\\{DateTime.Now.ToString("yyyyMMdd")}");
if (!Directory.Exists(folderPath)) if (!Directory.Exists(folderPath))
{ {
Directory.CreateDirectory(folderPath); Directory.CreateDirectory(folderPath);
@ -207,6 +204,23 @@ namespace Tiobon.Core.Common.LogHelper
case "RecordAccessLogs": case "RecordAccessLogs":
//TODO 是否需要Debug输出? //TODO 是否需要Debug输出?
Log.Information(logContent); Log.Information(logContent);
Task.Factory.StartNew(() =>
{
var requestInfo = JsonHelper.JsonToObj<UserAccessModel>(logContent);
if (requestInfo != null && requestInfo.API != "/api/file/upload")
{
DbInsert di = new DbInsert("Ghre_ApiLog");
di.Values("UserId", requestInfo.User);
di.Values("IP", requestInfo.IP);
di.Values("Path", requestInfo.API);
di.Values("Method", requestInfo.RequestMethod);
di.Values("RequestData", requestInfo.RequestData);
di.Values("BeginTime", requestInfo.BeginTime);
di.Values("OPTime", requestInfo.OPTime.Replace("ms", null));
di.Values("Agent", requestInfo.Agent);
DbAccess.ExcuteNonQuery(di.GetSql());
}
});
break; break;
case "SqlLog": case "SqlLog":
Log.Information(logContent); Log.Information(logContent);
@ -592,10 +606,10 @@ namespace Tiobon.Core.Common.LogHelper
rows = apiDates, rows = apiDates,
}; };
} }
} }
public enum ReadType public enum ReadType
{ {
/// <summary> /// <summary>
/// 精确查找一个 /// 精确查找一个
/// </summary> /// </summary>
@ -610,5 +624,4 @@ namespace Tiobon.Core.Common.LogHelper
/// 指定前缀,最新一个文件 /// 指定前缀,最新一个文件
/// </summary> /// </summary>
PrefixLatest PrefixLatest
}
} }

@ -13,6 +13,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Tiobon.Core.Model;
namespace Tiobon.Core.Extensions.Middlewares namespace Tiobon.Core.Extensions.Middlewares
{ {
@ -122,15 +123,4 @@ namespace Tiobon.Core.Extensions.Middlewares
} }
public class UserAccessModel
{
public string User { get; set; }
public string IP { get; set; }
public string API { get; set; }
public string BeginTime { get; set; }
public string OPTime { get; set; }
public string RequestMethod { get; set; }
public string RequestData { get; set; }
public string Agent { get; set; }
}
} }

@ -0,0 +1,14 @@
namespace Tiobon.Core.Model;
public class UserAccessModel
{
public string User { get; set; }
public string IP { get; set; }
public string API { get; set; }
public string BeginTime { get; set; }
public string OPTime { get; set; }
public string RequestMethod { get; set; }
public string RequestData { get; set; }
public string Agent { get; set; }
}
Loading…
Cancel
Save