Log4Net is writing "Invalid Date" to logs in Australia

Viewed 21

For security reasons, we are not allowed to have our log4net use any external config files (like app.config), so everything has to be coded internally.

We are using log4net to write logs to an SQLite database. Our date parameter looks like this:

adoNetAppender.AddParameter(new AdoNetAppenderParameter
{
    ParameterName = @"@date",
    DbType = DbType.DateTime,
    Layout = new RawUtcTimeStampLayout(),
});

Everything looked fine on this side of the pond, but many Australian users were seeing "Invalid Date" on every entry.

Figuring that it might be a timezone issue, I converted the event's timestamp to universal time, which I was hoping would fix the problem but we are still seeing it.

public virtual object Format(LoggingEvent loggingEvent)
{
    return loggingEvent.TimeStamp.ToUniversalTime();
}

Is there something we need to set internally for log4net to get a valid date, or is there something else that can be done to format it better? I am handicapped by the fact that it can't be reproduced here, no matter if we set our machines to Australian time or use various VMs to attempt to reconstruct the error.

1 Answers

SQLite uses strings for dates and times, which are affected by culture. It would appear that your SQLite ADO.NET provider is not using the InvariantCulture when creating a string from the raw DateTime placed in the ADO parameter by Log4Net's RawUtcTimestampLayout.

Since this looks like it was added to Microsoft.Data.Sqlite.Core back in 2018, I can only assume you are using a very old version, or a different provider.

I suggest you update to the latest version of your SQLite ADO.Net provider, either Microsoft.Data.Sqlite, or Microsoft.Data.Sqlite.Core.

It's probably also in the latest of System.Data.SQLite, but I didn't check since that has long been replaced with the Microsoft.* versions.

Your first Log4Net code sample is fine. You do not need the Format override.

Related