log4net to SQLServer : what happens if database is unavailable?
Asked Answered
S

3

10

I have a log4net ado appender writing to a SQL Server database. I like it, I think it's neat. Before I send it into production, I want to know what the behaviour will be if the database goes down.

I don't want the application to stop because the logging database is unavailable. I am presuming that log4net will just silently fail and not do anything, at least that's what I'm hoping. Can anyone confirm this or (better) point me to some documentation that will confirm this?

Sheridansherie answered 23/9, 2010 at 13:24 Comment(2)
Interesting question, but couldn't you test that simply by taking the database offline, or using connection information that specifies a database that doesn't exist?Guaiacol
Well I suppose I could, but there are loads of ways that the database could be unavaiable - it could have run out of space, or be running really slowly or being restarted or whatever. I can't test every scenario, so thought I would see if anyone knew for sure.Sheridansherie
R
14

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.

Romeo answered 23/9, 2010 at 13:33 Comment(3)
I've been using the ADO.net appender for my project. In prod things are great, but in our QA env, we get no activity for hours, which causes our DB connection to timeout. I've been trying to figure out the magic setting to fix this for months. Thanks for this post since it fixed my problem.Shirl
Connect Timout should have spelling mistake fixed to be Connect Timeout.Sandwich
If you want to improve performance without hitting the database as often increase the buffer size so messages are written in blocks. If you can live with your log not always being 100% current then do this: <bufferSize value="50" />Wappes
S
7

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/

Sandwich answered 12/7, 2016 at 15:54 Comment(0)
P
0

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.

Papillote answered 17/12, 2015 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.