can't save database caustom log in catch region of a try catch

Hi all,
I have a strange problem. Probably there is a reason but I cannot get it. 
I was asked by my client to develop a custom logger to database. It should be very simple and it is very simple.
I also created the table CCC_Logs to store the records.
The Logger I developed is an Vbnet object, it has a property (among the others) that is the Connection of type IConnection and a method (among the others) that is “WriteLog(…)”

Public Class CCC_Logs
Private Property Connection As IConnection

Public Sub New(ByRef connection As IConnection)
Me.Connection = connection
End Sub

Public Sub WriteLog(ByVal ccc_ Message as string)
Dim logRecord As ISingleDbObject = Me.Connection.CreateSingle("CCC_Logs")
logRecord ("CCC_Message").NewValue = ccc_Message
logRecord.Save()
end sub
End Class

The idea is to instantiate the object Logger passing the Connection and then call the method WriteLog to persist the info on the database. It works VERY WELL but it stops working if I try to log something into a catch section. I mean, if I do this:

Public sub xxxx()
Dim logger As CCC_Logs = New CCC_Logs(Connection)
logger.WriteLog(“Log 1!”)
Try
logger.WriteLog(“Log 2!”)
Catch
logger.WriteLog(“Log 3!”)
End try
End sub

If I run this simple code, on the database I only find "Log 1!" and "Log 2!" but it seems that "Log 3!" is not persisted, even if no error or exception are raised during the execution of the statement logger.WriteLog(“Log 3!”).
Please, how can I fix this? Why in the catch section the code stops working?

Version 8.12
Thank you in advance to anyone who can help
Alberto

  • Hi Alberto,

    Unless I'm being totally stupid "logger.WriteLog(“Log 3!”)" will only ever happen if there is an exception from the Try. So of course you won't see it.

    If there is code like this:

    If A = 1 Then

    Log1

    Else

    Log2

    End If

    And A=1 we'll only ever see Log1 ..... we only see Log2 if A<>1.

    Or have I completely misunderstood?

    HTH, Barry.

  • Hi Barry, you are totally right.
    The code I wrote is just an example as I cannot copy paste from the development environment.
    I forgot to specify that, in case of exception, when the process enter the catch, region, even if the instruction logger.WriteLog(“Log 3!”) is executed without problems (I can see it if I run the code in debug mode from Visual Studio 2019), the instruction does not save anything on the database.
    It happens only into the catch region.
    Sorry if I as not enough clear.
    Alberto

  • I am just asking, you are trying to log something using object-layer in the catch blog of a try-catch. And I assume that the reason you are running into the catch blog is also an object-layer operation? If so, then your log operation may be affected by the database transaction rollback initiated by the object layer.