问题
I use nlog dll to write to database - oracle with entity frameWork in the line :
logger.Log(logLevel, "try");
I get in the logs of nlog the following error:
The literal does not match the template string
the code is:
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>
I tryed in the Web.config to change the line:
<parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" />
to:
<parameter name="TIME_STAMP" layout="${longDate}" />
and I got the same error
Can anyone help?
回答1:
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.
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);
See also: https://github.com/NLog/NLog/wiki/EventProperties-Layout-Renderer
来源:https://stackoverflow.com/questions/59018213/add-record-in-nlog-to-field-with-datatype-date