将 nlog 中的记录添加到 dataType = date 的字段中 [英] add record in nlog to field with dataType = date
问题描述
我使用 nlog dll 写入数据库 - oracle with entity frameWork :
I use nlog dll to write to database - oracle with entity frameWork in the line :
logger.Log(logLevel, "try");
我在 nlog 的日志中得到以下错误:
I get in the logs of nlog the following error:
文字与模板字符串不匹配
The literal does not match the template string
代码是:
SetPropGDC(LogEntity);
NLog.LogLevel logLevel = SetLogLevel(Level.Debug);
logger.Log(logLevel, "try");
ClearGDC();
private void SetPropGDC(LogEntity LogEntity)
{
GlobalDiagnosticsContext.Set(processId, LogEntity.PROCESS_ID.ToString());
GlobalDiagnosticsContext.Set("TIME_STAMP", DateTime.Now);
GlobalDiagnosticsContext.Set(customerId, LogEntity.CUSTOMER_ID.ToString());
}
<targets>
<target name="TRACEDatabase" type="DataBase" keepConnection="false"
dbProvider="Oracle.ManagedDataAccess.Client" connectionString="${gdc:connectionString}"
commandText="insert into TLOG_SITE_GENERAL_TRACE( PROCESS_ID,TIME_STAMP,CUSTOMER_ID)
values(:PROCESS_ID,:TIME_STAMP,:CUSTOMER_ID)">
<parameter name="PROCESS_ID" layout="${gdc:PROCESS_ID}" />
<parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" />
<parameter name="CUSTOMER_ID" layout="${gdc:CUSTOMER_ID}" />
</target>
我尝试在 Web.config 中更改行:
I tryed in the Web.config to change the line:
<parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" />
到:
<parameter name="TIME_STAMP" layout="${longDate}" />
我也遇到了同样的错误
有人可以帮忙吗?
推荐答案
NLog DatabaseTarget 参数默认转换为字符串.您可以通过指定 dbType
来更改数据类型,使其与数据库列匹配:
NLog DatabaseTarget parameters converts to string by default. You can change the datatype by specifying dbType
so it matches the database-column:
<target name="TRACEDatabase" type="DataBase">
<parameter name="PROCESS_ID" layout="${event-properties:PROCESS_ID}" />
<parameter name="TIME_STAMP" layout="${date}" dbType="DateTime" />
<parameter name="CUSTOMER_ID" layout="${event-properties:CUSTOMER_ID}" />
</target>
顺便说一句.使用全局变量来传输特定于上下文的细节是一个坏主意.
Btw. it is a bad idea to use global variables for transfering context specific details.
相反,您应该使用 NLog LogEventInfo 属性:
Instead you should make use of NLog LogEventInfo Properties:
var logLevel = SetLogLevel(Level.Debug);
var theEvent = new NLog.LogEventInfo(logLevel, null, "try");
theEvent.Properties["PROCESS_ID"] = LogEntity.PROCESS_ID.ToString();
theEvent.Properties["CUSTOMER_ID"] = LogEntity.CUSTOMER_ID.ToString();
log.Log(theEvent);
另见:https://github.com/NLog/NLog/wiki/EventProperties-Layout-Renderer
这篇关于将 nlog 中的记录添加到 dataType = date 的字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!