- Feature request / Question
NLog version: 4.4.1
Platform: .Net 4.5 / Mono 4
Current NLog config (xml or C#, if relevant)
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Error" internalLogFile="nlog-internal.log">
. . .
<target name="DatabaseM" xsi:type="Database"
commandText="INSERT INTO myDb..myTable(Timestamp, Message) values(CONVERT(Datetime, @Timestamp, 120), @Message)"
connectionString="Data Source=192.168.0.1;Initial Catalog=myDB;User Id=myUser;Password=myPassword;MultipleActiveResultSets=true">
<parameter name="@Timestamp" layout="${date}"/>
<parameter name="@Message" layout="${message}"/>
. . .
(I don't think there is necessary full xml file)
Hello! Let's present that everything ok, database working, NLog logging. But then suddenly there is network issues and NLog can't see database to log. It would throw exception, but as I set flag throwExceptions="false", it won't. Application works fine. Then connection established and logging continued. BUT! There is important thing - while there was no connection to database all logs in this time just weren't sent. And this is pity. I just lost part of importand data, and it will not be stored in database (I mean logs, of course).
I could set throwExceptions="true", but then exception will be thrown as unhandled exception and application will be terminated...
I know that there is "internalLogFile" and similar params but I don't need file. I need write all logs to database. If connection came back, let it write all logs what weren't logged yet.
So, question: is there a way handle internal exceptions in C# code? To keep not-sended logs in memory and send them as soon as possible later when connection will be fine?
If there isn't then it's my feature request. Make "wait" all database logs while they throw exceptions about "timeout" or similar reason and try re-send it to make sure, they will be stored. If you make this, I would be sooo happy.
At now I see only 1 way - keep watching internal file log by timer and retry log this data, but it's not a 'right' way.
hey!
Don't lose logs!
You could use the Fallback wrapper?
If you log to the in case of a connection issue Memory-target, you could resend them later to the DB.
Guess the issue-title should be: Data lost during DB connection issues
Instead of the Fallback wrapper, then it could also be the RetryingWrapper with an infinite retries and sleeping every 1 sec.
Probably like this, so it doesn't become a blocking operation for the logger, in case of connection issues:
AsyncWrapper (BatchSize=1, OverflowAction=Grow) -> RetryingWrapper -> DatabaseTarget.
Even better solution!
Thank you so much!
I assume your question has been answered, if not, let us know!
Yes, it is and can be closed. Thank you again for your help!