log4net 没有将精确时间记录到数据库
log4net not logging precise time to DB
我通过 log4net 设置了数据库日志记录。我得到相同的时间戳并且没有异常对象。
追加器:
<appender name="AsyncDBAppender" type="DA.Systems.Focus.Shared.LogCore.AsynchronousAdoNetAppender, Focus-Shared-LogCore">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=dbServer;initial catalog=dbName;persist security info=True;user id=username;password=password;MultipleActiveResultSets=True;Connect Timeout=360;App=log4net" />
<commandText value="INSERT INTO [dbo].[MainLog] ([ProjectId], [ApplicationId], [MachineName],[OperationId], [UserId], [Date], [Thread] ,[Level], [Logger], [Message], [Exception])
VALUES (1, 2, @machineName, @operationId, @userId, @logDate, @thread, @logLevel, @logger, @message, @exception)" />
<parameter>
<parameterName value="@machineName" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{log4net:HostName}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operationId" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{OperationId}" />
</layout>
</parameter>
<parameter>
<parameterName value="@userId" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{UserId}" />
</layout>
</parameter>
<parameter>
<parameterName value="@logDate" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="@logLevel" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
生成此日志:
问题:
按日期排序会破坏实际顺序。模式是 log4net.Layout.RawTimeStampLayout
& 数据库列是 Datetime2
当出现异常时,异常本身没有被记录
爆破码:
try
{
InfoScope($"{LayerName} -> {callerInfo.MethodName} -> Passes bill created. Notifying Security-System...", userId, operationId);
var notificationResult = await _securitySystem.FocusBillPaymentNotification(billInfoBdo.BillDetails, userId, operationId);
if (notificationResult.Result)
{
InfoScope($"{LayerName} -> {callerInfo.MethodName} -> Notifying Security-System succeeded: Security-System Receipt #{notificationResult.SecuritySystemReceiptNumber}, Message: {notificationResult.Message}", userId, operationId);
}
else
{
WarnScope($"{LayerName} -> {callerInfo.MethodName} -> Notifying Security-System failed: {notificationResult.Message}", userId, operationId);
}
}
catch (Exception exp)
{
ErrorScope($"{LayerName} -> {callerInfo.MethodName} -> Exception at Security-System Payment notification [{exp.Message}]", exp, userId, operationId);
}
如何使时间戳更加精确?
为什么异常 pobject 不会得到 .ToString()/logged?
这是我使用的 Logging Core:
#region Error
[DebuggerStepThrough]
protected void ErrorScope(string message, string userId, Guid operationId)
{
SetUserId(userId);
SetOperationId(operationId);
Logger.ErrorFormat(message);
}
[DebuggerStepThrough]
protected void ErrorScope(string message, Exception exception, Guid operationId)
{
SetOperationId(operationId);
Logger.ErrorFormat(message, exception.GetInnerExceptions());
}
[DebuggerStepThrough]
protected void ErrorScope(string message, Exception exception, string userId, Guid operationId)
{
SetUserId(userId);
ErrorScope(message, exception, operationId);
}
#endregion
好吧,这不是问题的直接解决方案,但我确实设法以不同的方式获得了正确的时间戳:我向数据库添加了另一列,类型为 DateTime2
,默认为 SysDateTime()
:
RecordStamp DATETIME2 NOT NULL CONSTRAINT DF_MainLog_RecordStamp DEFAULT (SYSDATETIME())
现在,该专栏负责确保每个日志条目都具有有效的时间戳。
我通过 log4net 设置了数据库日志记录。我得到相同的时间戳并且没有异常对象。
追加器:
<appender name="AsyncDBAppender" type="DA.Systems.Focus.Shared.LogCore.AsynchronousAdoNetAppender, Focus-Shared-LogCore">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=dbServer;initial catalog=dbName;persist security info=True;user id=username;password=password;MultipleActiveResultSets=True;Connect Timeout=360;App=log4net" />
<commandText value="INSERT INTO [dbo].[MainLog] ([ProjectId], [ApplicationId], [MachineName],[OperationId], [UserId], [Date], [Thread] ,[Level], [Logger], [Message], [Exception])
VALUES (1, 2, @machineName, @operationId, @userId, @logDate, @thread, @logLevel, @logger, @message, @exception)" />
<parameter>
<parameterName value="@machineName" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{log4net:HostName}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operationId" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{OperationId}" />
</layout>
</parameter>
<parameter>
<parameterName value="@userId" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{UserId}" />
</layout>
</parameter>
<parameter>
<parameterName value="@logDate" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="@logLevel" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
生成此日志:
问题:
按日期排序会破坏实际顺序。模式是
log4net.Layout.RawTimeStampLayout
& 数据库列是 Datetime2当出现异常时,异常本身没有被记录
爆破码:
try
{
InfoScope($"{LayerName} -> {callerInfo.MethodName} -> Passes bill created. Notifying Security-System...", userId, operationId);
var notificationResult = await _securitySystem.FocusBillPaymentNotification(billInfoBdo.BillDetails, userId, operationId);
if (notificationResult.Result)
{
InfoScope($"{LayerName} -> {callerInfo.MethodName} -> Notifying Security-System succeeded: Security-System Receipt #{notificationResult.SecuritySystemReceiptNumber}, Message: {notificationResult.Message}", userId, operationId);
}
else
{
WarnScope($"{LayerName} -> {callerInfo.MethodName} -> Notifying Security-System failed: {notificationResult.Message}", userId, operationId);
}
}
catch (Exception exp)
{
ErrorScope($"{LayerName} -> {callerInfo.MethodName} -> Exception at Security-System Payment notification [{exp.Message}]", exp, userId, operationId);
}
如何使时间戳更加精确?
为什么异常 pobject 不会得到 .ToString()/logged?
这是我使用的 Logging Core:
#region Error
[DebuggerStepThrough]
protected void ErrorScope(string message, string userId, Guid operationId)
{
SetUserId(userId);
SetOperationId(operationId);
Logger.ErrorFormat(message);
}
[DebuggerStepThrough]
protected void ErrorScope(string message, Exception exception, Guid operationId)
{
SetOperationId(operationId);
Logger.ErrorFormat(message, exception.GetInnerExceptions());
}
[DebuggerStepThrough]
protected void ErrorScope(string message, Exception exception, string userId, Guid operationId)
{
SetUserId(userId);
ErrorScope(message, exception, operationId);
}
#endregion
好吧,这不是问题的直接解决方案,但我确实设法以不同的方式获得了正确的时间戳:我向数据库添加了另一列,类型为 DateTime2
,默认为 SysDateTime()
:
RecordStamp DATETIME2 NOT NULL CONSTRAINT DF_MainLog_RecordStamp DEFAULT (SYSDATETIME())
现在,该专栏负责确保每个日志条目都具有有效的时间戳。