0

I am trying to write to my database the date of now with NLog and I have this error:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

I tried everything but nothing works for me and even when I print on the terminal the date has a correct format. I am using DateTime.Now so I don't really know.

Controller :

var modificationDate = new SqlDateTime(DateTime.Now);

// Créer un LogEventInfo
var logEvent = new LogEventInfo(NLog.LogLevel.Info, logger.Name, "Accès modifiés pour l'utilisateur {UserID}");
logEvent.Properties["UserID"] = userID;
logEvent.Properties["currentUser"] = currentUser;
logEvent.Properties["ModificationDate"] = modificationDate;
logEvent.Properties["UserGroups"] = string.Join(",", groups);

NLog.config :

<parameter name="@util" layout="${event-properties:UserID}" dbType="String" size="50" />
<parameter name="@modif_par" layout="${event-properties:currentUser}" dbType="String" size="50" />
<parameter name="@date_modif" layout="${longdate:universalTime=true}" dbType="datetime" />
<parameter name="@acces_modif" layout="${event-properties:UserGroups}" dbType="String" />
3
  • 2
    The default value for a date in C# is less than the lowest allowable value for a datetime in SQL Server. So it's not getting passed to NLog. I haven't used NLog myself but I noticed in your config that the @date_modif parameter doesn't have an event-properties in the layout? Maybe that's why the date doesn't have a value? Commented Oct 9, 2024 at 13:16
  • 2
    One separate concern I noticed is @date_modif is configured for universal time, but you are assigning it a value created for the local time via DateTime.Now. You may want DateTime.UtcNow instead, or may want to change the definition. Of course, if you know the code is running on a machine configured where the local time is already universal time this is less important. Commented Oct 9, 2024 at 13:36
  • What version of NLog are you using? It is always good idea to use throwConfigExceptions="true" and enable NLog InternalLogger when troubleshooting NLog. Commented Oct 9, 2024 at 19:38

1 Answer 1

0

DateTime Handling: -

Remove SqlDateTime conversion in the controller
Use DateTime.UtcNow instead of DateTime.Now
Store the date directly as a DateTime object in the Properties

NLog Configuration:

Change the dbType to "DateTime2" instead of "datetime"
Use ${event-properties:ModificationDate} instead of ${longdate}
Remove the :universalTime=true from the layout

Sign up to request clarification or add additional context in comments.

2 Comments

Changing to datetime2 will only cover up the real error - the log is trying to write something like 0001-01-01 instead of the actual date.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.