I am trying to log some audit information to a SQL Server 2008 table using NLog 2. In order to be able to pass parameters to the SQL insert query, I'm using LogEventInfo and the Event Context Layout Renderer.
The logging itself works but the datetime is stored with only second precision. I want to be able to store with millisecond precision but have not found anything that shows me how to do this.
This is the C# code where I log the event:
private void LogMessage(DateTime requestDateTime)
{
LogEventInfo theEvent = new LogEventInfo(LogLevel.Debug, "", "Pass my custom value");
theEvent.Properties["RequestDate"] = requestDateTime;
}
This is the target 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 I have found using theEvent.Properties["RequestDate"] = requestDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")
but I would prefer not to have to do this because then you may run into problems with date time formatting and culture.
Does anyone know of a way that I can change the precision with config in NLog.config?