Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AdoNetAppender using npgsql #71

Merged
merged 1 commit into from
Dec 8, 2020

Conversation

NicholasNoise
Copy link

@NicholasNoise NicholasNoise commented Dec 7, 2020

Using npgsql on netfx and mono I've encountered this error on an applicaiton startup:

log4net:ERROR [CustomAdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database
Npgsql.PostgresException (0x80004005): 42601: syntax error at or near ":"
   at Npgsql.NpgsqlConnector.DoReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isPrependedMessage)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlConnector.ReadExpecting[T](Boolean async)
   at Npgsql.NpgsqlCommand.Prepare()
   at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)

The error prevents to write only very first logging event, because Npgsql fails to prepare command with no parameters (at a moment). So it is not a problem in general, but disturbing.

I think my fix is not that good but does a trick.

An alternative is to disable dbCmd.Prepare(); if it fails once, but it is overkill, cuz there is no second fail.

					if (m_doCommandPrepare)
					{
						try
						{
							// prepare the command, which is significantly faster
							dbCmd.Prepare();
						}
						catch (Exception)
						{
							m_doCommandPrepare = false;
						}
					}
...
		private bool m_doCommandPrepare = true;

@NicholasNoise
Copy link
Author

@fluffynuts
At a look please.

@fluffynuts fluffynuts merged commit 2b8b170 into apache:master Dec 8, 2020
@fluffynuts fluffynuts deleted the fix-npgsql-appender branch December 8, 2020 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants