Hi,I’ve implemented Log4Xojo recently on my windows desktop application and finding it really good and easy to use.
However, when I need to debug/step through code from a breakpoint, execution gets hung up on the thread(s) of the logging execution and I’m finding it difficult to debug any code because of it.
Is there a way to somehow disable debugging of a module (Log4Xojo module) or even just disable the logging altogether without, for example, commenting out all the log4xojo statements. Any suggestions welcome.
Thanks
Yup, when debugging in the IDE you don’t really need the functionality of Log4Xojo… so you can use Xojo’s compiler directives to skip it while in the IDE: DebugBuild — Xojo documentation
Thanks. So am I right in thinking this would involve adding the compiler directives around all 60 (currently) log statements?
Public Sub Log(message As String, level As LogLevel, Optional location As String = "")
#If Not DebugBuild Then
// Check if the current message level is high enough to be logged
If Integer(level) < Integer(mCurrentLogLevel) Then
Return // Don't log messages below the current log level
End If
// Prepare the log message with timestamp and log level
Var formattedMessage As String
Var timestamp As String
timestamp = DateTime.Now.SQLDateTime
Var l As String = StringValue(level)
// Construct the message with optional location
If location = "" Then
formattedMessage = "[" + timestamp + "] [" + l + "] " + message
Else
formattedMessage = "[" + timestamp + "] [" + location + "] [" + l + "] " + message
End If
// Existing logging logic remains the same
For Each destination As LogDestination In mLogDestinations
Select Case destination
Case LogDestination.DebugLog
System.DebugLog(formattedMessage)
Case LogDestination.SystemLog
System.Log(SystemLogLevelFromLogLevel(level), formattedMessage)
Case LogDestination.FileLog
// Use mutex when adding to queue
mLogQueueMutex.Enter
Try
mLogQueue.Add(formattedMessage)
Finally
mLogQueueMutex.Leave
End Try
Case LogDestination.All
System.DebugLog(formattedMessage)
System.Log(SystemLogLevelFromLogLevel(level), formattedMessage)
// Use mutex when adding to queue
// Optional: Prevent queue from growing too large
mLogQueueMutex.Enter
Try
If mLogQueue.Count >= MaxQueueSize Then
// Optionally: Remove oldest message to make room
mLogQueue.RemoveAt(0)
End If
// Add new message
mLogQueue.Add(formattedMessage)
Finally
mLogQueueMutex.Leave
End Try
End Select
Next
#Else
system.DebugLog(message)
#EndIf
End Sub
overwrite the current Log function, found in the Log4Xojo class. It’s a quick hack that might help you, without the need of modifying all your 60 calls
Perfect! I’ll give it a go, thanks.
Thought this solved my issue but when I’ve tried stepping through my code from a breakpoint (more than just a couple of steps), execution flips to the Log4Xojo LogthreadHandler method. I guess I’m looking to disable the whole thing whilst debugging?
yep, disable log4xojo while in the IDE.
What’s the easiest way of doing this, if that’s not a stupid question sorry?
If Not DebugBuild Then
...
End If
There are no stupid questions here.
So i’ve previously added If Not Debugbuild.. as suggested, around the Log method but this isn’t disabling Log4Xojo as such. Not sure how to completely disable it?
Within the Log Method, i would recommend using an If DebugBuild Then Return
instead to avoid unnecessary nesting.
Back to your question, you probably need to add an If Not DebugBuild Then XYZ.Log(...)
at the points where the Log method is called? Or the Debugger will jump to the Log Method everytime.
I for example wrote a Public “ReportError” Method in a Module and used this to capture errors using Log4Xojo. In my case i would only have to add a If Not DebugBuild...
to this “ReportError” Method.
Public Sub ReportError(err As DatabaseException, CurrentMethod As String)
App.AppProtocoll.Log(err.Message + " (File: " + Chr(34) + Self.DatabaseFile.NativePath + Chr(34) + " / Database: " + Self.DatabaseName + ")." + EndOfLine + _
"Code: " + err.ErrorNumber.ToString + EndOfLine + _
"Method: " + CurrentMethod + EndOfLine + _
"Version: " + WAG5Versionsnummer + EndOfLine + _
"Stack: " + String.FromArray(err.Stack, EndOfLine), Class_Log4Xojo.LogLevel.Error)
End Sub
I too have a ShowError method but I only use this for ‘errors’ - unfortunately I’ve added many debug and info level logging statements throughout my code (really loving Log4Xojo )
What I’m seeing though is that the Log4Xojo/LogThreadHandler method is running even though I’m bypassing the specific Log method using If Not Debugbuild..
as above. Is that not correct?
(This is evident when stepping through my code and then execution stepping into LogThreadHandler)
You could disable the Constructor of Class_Log4Xojo using If DebugBuild Then Return
in the first line of the Code. But I can’t say whether that won’t cause other problems. It’s worth a try.
Patch uploaded to GitHub to disable asynchronous file logging while debugging.
Much appreciated! Works like a charm