Merge pull request #312 from Jamnine/master

日志审计加入TraceId
This commit is contained in:
ansonzhang 2022-11-26 14:10:17 +08:00 committed by GitHub
commit a22118afad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 35 additions and 15 deletions

View File

@ -386,6 +386,11 @@
ID
</summary>
</member>
<member name="P:Blog.Core.Model.Models.GblLogAudit.TraceId">
<summary>
HttpContext.TraceIdentifier 事件链路ID获取或设置一个唯一标识符用于在跟踪日志中表示此请求。
</summary>
</member>
<member name="P:Blog.Core.Model.Models.GblLogAudit.Date">
<summary>
时间

View File

@ -7,7 +7,7 @@
<connectionType value="Microsoft.Data.Sqlite.SqliteConnection, Microsoft.Data.Sqlite, Version=7.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" />
<connectionStringName value="sqlite" />
<connectionString value="Data Source=WMBlog.db" />
<commandText value="INSERT INTO GblLogAudit ([Date],[Thread],[Level],[Logger],[LogType],[DataType],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger,@logType,@dataType, @message, @exception)" />
<commandText value="INSERT INTO GblLogAudit ([Date],[Thread],[Level],[Logger],[LogType],[DataType],[Message],[Exception],[TraceId]) VALUES (@log_date, @thread, @log_level, @logger,@logType,@dataType, @message, @exception,@traceId)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
@ -67,6 +67,14 @@
<size value="999999999" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<parameter>
<parameterName value="@traceId" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{TraceId}" />
</layout>
</parameter>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />

View File

@ -23,7 +23,7 @@ namespace Blog.Core.Common.LogHelper
_contentRoot = contentPath;
}
public static void OutLogAOP(string prefix, string[] dataParas, bool IsHeader = true, bool isWrt = false)
public static void OutLogAOP(string prefix, string traceId, string[] dataParas, bool IsHeader = true)
{
string AppSetingNodeName = "AppSettings";
string AppSetingName = "LogAOP";
@ -57,11 +57,11 @@ namespace Blog.Core.Common.LogHelper
{
if (AppSettings.app(new string[] { AppSetingNodeName, AppSetingName, "LogToDB", "Enabled" }).ObjToBool())
{
OutSql2LogToDB(prefix, dataParas, IsHeader);
OutSql2LogToDB(prefix, traceId, dataParas, IsHeader);
}
if (AppSettings.app(new string[] { AppSetingNodeName, AppSetingName, "LogToFile", "Enabled" }).ObjToBool())
{
OutSql2LogToFile(prefix, dataParas, IsHeader);
OutSql2LogToFile(prefix, traceId, dataParas, IsHeader);
}
}
@ -75,7 +75,7 @@ namespace Blog.Core.Common.LogHelper
//}
}
public static void OutSql2LogToFile(string prefix, string[] dataParas, bool IsHeader = true, bool isWrt = false)
public static void OutSql2LogToFile(string prefix, string traceId, string[] dataParas, bool IsHeader = true, bool isWrt = false)
{
try
{
@ -170,9 +170,10 @@ namespace Blog.Core.Common.LogHelper
LogWriteLock.ExitWriteLock();
}
}
public static void OutSql2LogToDB(string prefix, string[] dataParas, bool IsHeader = true)
public static void OutSql2LogToDB(string prefix, string traceId, string[] dataParas, bool IsHeader = true)
{
log4net.LogicalThreadContext.Properties["LogType"] = prefix;
log4net.LogicalThreadContext.Properties["TraceId"] = traceId;
if (dataParas.Length >= 2)
{
log4net.LogicalThreadContext.Properties["DataType"] = dataParas[0];

View File

@ -149,7 +149,7 @@ namespace Blog.Core.AOP
Parallel.For(0, 1, e =>
{
//LogLock.OutLogAOP("AOPLog", new string[] { dataIntercept });
LogLock.OutLogAOP("AOPLog", new string[] { apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo) });
LogLock.OutLogAOP("AOPLog", _accessor.HttpContext?.TraceIdentifier, new string[] { apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo) });
});
}
}
@ -189,7 +189,7 @@ namespace Blog.Core.AOP
Parallel.For(0, 1, e =>
{
//LogLock.OutSql2Log("AOPLog", new string[] { JsonConvert.SerializeObject(apiLogAopInfo) });
LogLock.OutLogAOP("AOPLog", new string[] { apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo) });
LogLock.OutLogAOP("AOPLog", _accessor.HttpContext?.TraceIdentifier, new string[] { apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo) });
});
});
}
@ -212,7 +212,7 @@ namespace Blog.Core.AOP
Parallel.For(0, 1, e =>
{
//LogLock.OutLogAOP("AOPLogEx", new string[] { dataIntercept });
LogLock.OutLogAOP("AOPLogEx", new string[] { apiLogAopExInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopExInfo) });
LogLock.OutLogAOP("AOPLogEx", _accessor.HttpContext?.TraceIdentifier, new string[] { apiLogAopExInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopExInfo) });
});
}

View File

@ -60,7 +60,7 @@ namespace Blog.Core.Extensions.Middlewares
Parallel.For(0, 1, e =>
{
//LogLock.OutSql2Log("RequestIpInfoLog", new string[] { requestInfo + "," }, false);
LogLock.OutLogAOP("RequestIpInfoLog", new string[] { requestInfo.GetType().ToString(), requestInfo }, false);
LogLock.OutLogAOP("RequestIpInfoLog", context.TraceIdentifier, new string[] { requestInfo.GetType().ToString(), requestInfo }, false);
});
//try

View File

@ -109,7 +109,7 @@ namespace Blog.Core.Extensions.Middlewares
Parallel.For(0, 1, e =>
{
//LogLock.OutSql2Log("RecordAccessLogs", new string[] { requestInfo + "," }, false);
LogLock.OutLogAOP("RecordAccessLogs", new string[] { userAccessModel.GetType().ToString(), requestInfo }, false);
LogLock.OutLogAOP("RecordAccessLogs", context.TraceIdentifier, new string[] { userAccessModel.GetType().ToString(), requestInfo }, false);
});
//var logFileName = FileHelper.GetAvailableFileNameWithPrefixOrderSize(_environment.ContentRootPath, "RecordAccessLogs");
//SerilogServer.WriteLog(logFileName, new string[] { requestInfo + "," }, false);

View File

@ -102,7 +102,7 @@ namespace Blog.Core.Extensions.Middlewares
Parallel.For(0, 1, e =>
{
//LogLock.OutSql2Log("RequestResponseLog", new string[] { "Request Data:", content });
LogLock.OutLogAOP("RequestResponseLog", new string[] { "Request Data - RequestJsonDataType:" + requestResponse.GetType().ToString(), content });
LogLock.OutLogAOP("RequestResponseLog", context.TraceIdentifier, new string[] { "Request Data - RequestJsonDataType:" + requestResponse.GetType().ToString(), content });
});
//SerilogServer.WriteLog("RequestResponseLog", new string[] { "Request Data:", content });
@ -125,7 +125,7 @@ namespace Blog.Core.Extensions.Middlewares
Parallel.For(0, 1, e =>
{
//LogLock.OutSql2Log("RequestResponseLog", new string[] { "Response Data:", ResponseBody });
LogLock.OutLogAOP("RequestResponseLog", new string[] { "Response Data - ResponseJsonDataType:" + responseBody.GetType().ToString(), responseBody });
LogLock.OutLogAOP("RequestResponseLog", response.HttpContext.TraceIdentifier, new string[] { "Response Data - ResponseJsonDataType:" + responseBody.GetType().ToString(), responseBody });
});
//SerilogServer.WriteLog("RequestResponseLog", new string[] { "Response Data:", responseBody });

View File

@ -67,7 +67,7 @@ namespace Blog.Core.Extensions
{
MiniProfiler.Current.CustomTiming("SQL", GetParas(p) + "【SQL语句】" + sql);
//LogLock.OutSql2Log("SqlLog", new string[] { GetParas(p), "【SQL语句】" + sql });
LogLock.OutLogAOP("SqlLog", new string[] { sql.GetType().ToString(), GetParas(p), "【SQL语句】" + sql });
LogLock.OutLogAOP("SqlLog","", new string[] { sql.GetType().ToString(), GetParas(p), "【SQL语句】" + sql });
});
}

View File

@ -15,6 +15,12 @@ namespace Blog.Core.Model.Models
[SugarColumn(ColumnDescription = "ID", IsNullable = false, IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
///<summary>
///HttpContext.TraceIdentifier 事件链路ID获取或设置一个唯一标识符用于在跟踪日志中表示此请求。
///</summary>
[SugarColumn(ColumnDescription = "事件链路ID", IsNullable = false, IsPrimaryKey = false, IsIdentity = false, ColumnDataType = "varchar", Length = 255)]
public string TraceId { get; set; }
///<summary>
///时间
///</summary>

View File

@ -93,7 +93,7 @@ namespace Blog.Core.Tasks
Parallel.For(0, 1, e =>
{
LogLock.OutLogAOP("ACCESSTRENDLOG", new string[] { activeUserVMs.GetType().ToString(), JsonConvert.SerializeObject(activeUserVMs) }, false, true);
LogLock.OutLogAOP("ACCESSTRENDLOG","",new string[] { activeUserVMs.GetType().ToString(), JsonConvert.SerializeObject(activeUserVMs) }, false);
});
}