I am trying to write some audit data to a SQL Server 2008 table using NLog 2. To be able to pass parameters to a SQL query, I use LogEventInfo and the Event Context Context Mapper .
The log itself works, but the datetime is saved only with a second precision. I want to be able to store with millisecond precision, but have not found anything that will show me how to do this.
This is the C # code where I register the event:
private void LogMessage(DateTime requestDateTime) { LogEventInfo theEvent = new LogEventInfo(LogLevel.Debug, "", "Pass my custom value"); theEvent.Properties["RequestDate"] = requestDateTime; }
This is the goal that I have in my NLog.config configuration:
<target xsi:type="Database" name="SqlLog" dbProvider="sqlserver" connectionString="server=localhost;database=Test;integrated security=sspi"> <commandText> INSERT [dbo].[ApiLog] ( [ServerName], [RequestDate] ) VALUES (@ServerName, @RequestDate); </commandText> <parameter name="@ServerName" layout="${machinename}"/> <parameter name="@RequestDate" layout="${event-context:item=RequestDate}"/> </target>
There is a workaround that I found using theEvent.Properties["RequestDate"] = requestDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") , but I would prefer not to, because then You may have problems with date formatting and date culture.
Does anyone know how I can change the accuracy using config in NLog.config?
source share