|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
using EntrustSettle.Common.Helper;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
@ -10,6 +10,7 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
{
|
|
|
|
|
public class LogLock
|
|
|
|
|
{
|
|
|
|
|
static Logger logger = null;
|
|
|
|
|
static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
|
|
|
|
|
static int WritedCount = 0;
|
|
|
|
|
static int FailedCount = 0;
|
|
|
|
@ -23,15 +24,13 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
public static void OutLogAOP(string prefix, string traceId, string[] dataParas, bool IsHeader = true)
|
|
|
|
|
{
|
|
|
|
|
string AppSetingNodeName = "AppSettings";
|
|
|
|
|
string AppSetingName = "LogAOP";
|
|
|
|
|
string AppSetingName = "";
|
|
|
|
|
switch (prefix)
|
|
|
|
|
{
|
|
|
|
|
// AOP中使用
|
|
|
|
|
case "AOPLog":
|
|
|
|
|
AppSetingName = "LogAOP";
|
|
|
|
|
break;
|
|
|
|
|
case "AOPLogEx":
|
|
|
|
|
AppSetingName = "LogAOP";
|
|
|
|
|
// 自定义的AOP中使用
|
|
|
|
|
case "ServiceAOPLog":
|
|
|
|
|
case "ServiceAOPLogEx":
|
|
|
|
|
AppSetingName = "ServiceAOPLog";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// 中间件中使用
|
|
|
|
@ -48,9 +47,9 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
AppSetingName = "RequestResponseLog";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// 暂时用不到
|
|
|
|
|
case "SqlLog":
|
|
|
|
|
AppSetingName = "SqlAOP";
|
|
|
|
|
// SqlSugarAOP中使用
|
|
|
|
|
case "SqlAOPLog":
|
|
|
|
|
AppSetingName = "SqlAOPLog";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
@ -58,15 +57,14 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
|
|
|
|
|
if (AppSettings.app(new string[] { AppSetingNodeName, AppSetingName, "Enabled" }).ObjToBool())
|
|
|
|
|
{
|
|
|
|
|
if (AppSettings.app(new string[] { AppSetingNodeName, AppSetingName, "LogToDB", "Enabled" }).ObjToBool())
|
|
|
|
|
{
|
|
|
|
|
OutSql2LogToDB(prefix, traceId, dataParas, IsHeader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AppSettings.app(new string[] { AppSetingNodeName, AppSetingName, "LogToFile", "Enabled" }).ObjToBool())
|
|
|
|
|
{
|
|
|
|
|
OutSql2LogToFile(prefix, traceId, dataParas, IsHeader);
|
|
|
|
|
}
|
|
|
|
|
if (AppSettings.app(new string[] { AppSetingNodeName, AppSetingName, "LogToDB", "Enabled" }).ObjToBool())
|
|
|
|
|
{
|
|
|
|
|
OutSql2LogToDB(prefix, traceId, dataParas, IsHeader);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -80,7 +78,7 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
// 因进入与退出写入模式应在同一个try finally语句块内,所以在请求进入写入模式之前不能触发异常,否则释放次数大于请求次数将会触发异常
|
|
|
|
|
LogWriteLock.EnterWriteLock();
|
|
|
|
|
|
|
|
|
|
var folderPath = Path.Combine(_contentRoot, "Logs", DateTime.Now.ToString("yyyyMMdd"));
|
|
|
|
|
var folderPath = Path.Combine(_contentRoot, "Logs", DateTime.Now.ToString("yyyy-MM-dd"));
|
|
|
|
|
if (!Directory.Exists(folderPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(folderPath);
|
|
|
|
@ -90,7 +88,7 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
var logFilePath = FileHelper.GetAvailableFileWithPrefixOrderSize(folderPath, prefix, 5);
|
|
|
|
|
switch (prefix)
|
|
|
|
|
{
|
|
|
|
|
case "AOPLog":
|
|
|
|
|
case "ServiceAOPLog":
|
|
|
|
|
AOPLogInfo apiLogAopInfo = JsonConvert.DeserializeObject<AOPLogInfo>(dataParas[1]);
|
|
|
|
|
//记录被拦截方法信息的日志信息
|
|
|
|
|
var dataIntercept = "" +
|
|
|
|
@ -104,7 +102,7 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
$"【执行完成结果】:{apiLogAopInfo.ResponseJsonData}\r\n";
|
|
|
|
|
dataParas = new string[] { dataIntercept };
|
|
|
|
|
break;
|
|
|
|
|
case "AOPLogEx":
|
|
|
|
|
case "ServiceAOPLogEx":
|
|
|
|
|
AOPLogExInfo apiLogAopExInfo = JsonConvert.DeserializeObject<AOPLogExInfo>(dataParas[1]);
|
|
|
|
|
var dataInterceptEx = "" +
|
|
|
|
|
$"【操作时间】:{apiLogAopExInfo.ApiLogAopInfo.RequestTime}\r\n" +
|
|
|
|
@ -121,14 +119,11 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
string logContent = String.Join("\r\n", dataParas);
|
|
|
|
|
string logContent = null;
|
|
|
|
|
if (IsHeader)
|
|
|
|
|
{
|
|
|
|
|
logContent = (
|
|
|
|
|
"--------------------------------\r\n" +
|
|
|
|
|
DateTime.Now + "|\r\n" +
|
|
|
|
|
String.Join("\r\n", dataParas) + "\r\n"
|
|
|
|
|
$"--------------------------------\r\n{DateTime.Now:yyyy-MM-dd HH:mm:ss:fff} TraceId:{traceId}\r\n{String.Join("\r\n", dataParas)}\r\n"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
@ -170,6 +165,11 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
|
|
|
|
|
public static void OutSql2LogToDB(string prefix, string traceId, string[] dataParas, bool IsHeader = true)
|
|
|
|
|
{
|
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
|
|
|
|
logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataParas = dataParas.Skip(1).ToArray();
|
|
|
|
|
|
|
|
|
|
string logContent = String.Join("", dataParas);
|
|
|
|
@ -181,26 +181,23 @@ namespace EntrustSettle.Common.LogHelper
|
|
|
|
|
switch (prefix)
|
|
|
|
|
{
|
|
|
|
|
//DEBUG | INFO | WARN | ERROR | FATAL
|
|
|
|
|
case "AOPLog":
|
|
|
|
|
Log.Information(logContent);
|
|
|
|
|
case "ServiceAOPLog":
|
|
|
|
|
logger.Info(logContent);
|
|
|
|
|
break;
|
|
|
|
|
case "AOPLogEx":
|
|
|
|
|
Log.Error(logContent);
|
|
|
|
|
case "ServiceAOPLogEx":
|
|
|
|
|
logger.Error(logContent);
|
|
|
|
|
break;
|
|
|
|
|
case "RequestIpInfoLog":
|
|
|
|
|
//TODO 是否需要Debug输出?
|
|
|
|
|
Log.Information(logContent);
|
|
|
|
|
logger.Info(logContent);
|
|
|
|
|
break;
|
|
|
|
|
case "RecordAccessLogs":
|
|
|
|
|
//TODO 是否需要Debug输出?
|
|
|
|
|
Log.Information(logContent);
|
|
|
|
|
logger.Info(logContent);
|
|
|
|
|
break;
|
|
|
|
|
case "RequestResponseLog":
|
|
|
|
|
//TODO 是否需要Debug输出?
|
|
|
|
|
Log.Information(logContent);
|
|
|
|
|
logger.Info(logContent);
|
|
|
|
|
break;
|
|
|
|
|
case "SqlLog":
|
|
|
|
|
Log.Information(logContent);
|
|
|
|
|
case "SqlAOPLog":
|
|
|
|
|
logger.Info(logContent);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|