Log4NetAdoNetAppender icon indicating copy to clipboard operation
Log4NetAdoNetAppender copied to clipboard

Issue when setting ConnectionString at runtime

Open jantoas opened this issue 1 year ago • 4 comments

Hello,

we have found following issue:

Error, when debugging log4net: log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Could not open database connection []. Connection string context [Unable to resolve connection string from ConnectionString, ConnectionStrings, or AppSettings.]. System.InvalidOperationException: The ConnectionString property has not been initialized. at System.Data.SqlClient.SqlConnection.PermissionDemand() at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at MicroKnights.Logging.AdoNetAppender.InitializeDatabaseConnection()

Setting connection string at runtime. Although connection string has been set as desired (after setting AdoNetAppender.ConnectionString will bee checked) the above error occurs.

Using .net 6 Application. SQL Server database.

Example connection string: data source=myserver\myinstance;initial catalog=INTERN_TEST;integrated security=false;persist security info=True;User ID=myuser;Password=mypassword

Application with WebApi and multithreading. Multiple log entries per second possible.

Example Code: adoNetAppender.ConnectionString = connectionStringFromConfig; adoNetAppender.ActivateOptions(); // here the error occurs (not thrown since handled exception, but effect is, that log entry is missing - error only shows, when log4net debugging active)

Are there any hints how to solve this issue?

Regards

Jörg

jantoas avatar Nov 09 '23 16:11 jantoas

Hi, just so i understand the context.

  1. Does the error happens when you set the connection at runtime, or before?
  2. Does it connect to the database and log?
  3. Is it only a question about "remove" the error log?

Cheers, Frank

microknights avatar Nov 18 '23 08:11 microknights

Hello Frank,

the issue happens, when connecting at runtime. At first we set database ConnectionString property. No exception here. Error happens when calling ActivateOptions(). It seems that ConnectionString is empty, when calling ActivateOptions() (I stepped into the function and internal called functions), so exception is thrown. Its strange, because, if I check ConnectionString property before and after ActivateOptions() it never is empty!

If exception occurs, no entry will be written to database.

It would be good, if it could be fixed. So, that log entry will be written to database. But I see, that it could be difficult to find source of above strange behavior. The error log to Log4Net Debugger is not the problem.

Regards

Jörg

Von: Frank Løvendahl Nielsen @.> Gesendet: Samstag, 18. November 2023 09:34 An: microknights/Log4NetAdoNetAppender @.> Cc: Antoni Jörg @.>; Author @.> Betreff: Re: [microknights/Log4NetAdoNetAppender] Issue when setting ConnectionString at runtime (Issue #42)

Hi, just so i understand the context.

  1. Does the error happens when you set the connection at runtime, or before?
  2. Does it connect to the database and log?
  3. Is it only a question about "remove" the error log?

Cheers, Frank

Reply to this email directly, view it on GitHubhttps://github.com/microknights/Log4NetAdoNetAppender/issues/42#issuecomment-1817445334, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BD3G3IUARZZ2QQKXBN4LKHDYFBXIXAVCNFSM6AAAAAA7E2FE6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJXGQ2DKMZTGQ. You are receiving this because you authored the thread.Message ID: @.***>

jantoas avatar Nov 21 '23 09:11 jantoas

ok, i will try do some test to provoke the issue you have. Can you deliver a code snippet of the startup of you log4net.

microknights avatar Nov 25 '23 08:11 microknights

Example Code is in first post. Since there are several threads possible, that code can be executed by more threads at same time.

adoNetAppender.ConnectionString = connectionStringFromConfig; adoNetAppender.ActivateOptions();

jantoas avatar Nov 28 '23 09:11 jantoas