log4net to SQLServer : what happens if database is unavailable?

限于喜欢 提交于 2019-11-28 10:11:22

The appender (like all log4net appenders that I am aware of) will fail silently and thus stop logging. You can configure the appender to try to reconnect though:

<reconnectonerror value="True" />

In that case you probably want to specify a connection time out in your db connection string:

Connect Timeout=1

If you do not do that your application will be very slow if the db is offline.

If in .Net 4.5.1 or later you will also have to set ConnectRetryCount=0; in your connection string.

Appender Configuration:

<ReconnectOnError value="true" />
<connectionString value="...Connect Timeout=1;ConnectRetryCount=0;" />

If you do Async logging like Log4Net.Async then Connect Timeout may be left at default 15 seconds.

Details

.Net 4.5.1 adds ADO.NET connection resiliency which tells Log4Net the connection is still open even though it isn't and attempts to reconnect using ConnectRetryCount. However since we want to let Log4Net do the reconnecting and ConnectRetryCount has a max of 255 we should set ConnectRetryCount to 0. Source: https://issues.apache.org/jira/browse/LOG4NET-442 https://blogs.msdn.microsoft.com/dotnet/2013/06/26/announcing-the-net-framework-4-5-1-preview/

using reconnectonerror will surely enable you to start logging again once the DB server is available, but the intermediate log messages, while the server was down, will be lost. I think log4Net doesn't have any provision to persist the messages util the DB server is available.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!