VB6: Can't do any logging with my ActiveX dll
I am trying to add logging to a dll that seems to be get stuck running. I have tried simply adding a couple subroutines to log to a file:
Sub PrepareLogging(ByVal strAppName As String)
开发者_如何学C Dim objFS As FileSystemObject
Dim objFolder As Folder
Dim objFile As File
Set objFS = New Scripting.FileSystemObject
g_strAppName = strAppName
If Not objFS.FolderExists(strLOG_PATH & g_strAppName) Then
objFS.CreateFolder (strLOG_PATH & g_strAppName)
End If
Set objFolder = objFS.GetFolder(strLOG_PATH & g_strAppName)
For Each objFile In objFolder.Files
If (Now - objFile.DateCreated) > 60 Then
objFile.Delete
End If
Next
Set objFolder = Nothing
Set objFS = Nothing
End Sub
Sub LogAction(ByVal strMsg As String)
Dim objFS As FileSystemObject
Dim objText As TextStream
Set objFS = New Scripting.FileSystemObject
Set objText = objFS.OpenTextFile(strLOG_PATH & g_strAppName & "\" & Format(Date, "yyyymmdd") & ".log", ForAppending, True)
objText.WriteLine (Format(Time, "hh:mm:ss") & " - " & strMsg)
objText.Close
Set objText = Nothing
Set objFS = Nothing
End Sub
The code will run, but it seems to just ignore all this logging functionality. I know the code is correct, because it works in other dlls and exes where I've added the module with these subroutines. I tried adding a basic command to log directly to the event log:
App.LogEvent strMsg
But that fails. If I try to catch errors in the code with a Goto ErrorHandler section:
ErrorHandler:
App.LogEvent Err.Number & Err.Description
The app completely fails, such that I can't even select any options after logging into the web service. I know that the problem is not the application log being full, as it continues to have new events added to it. Any suggestions or ideas on what is causing this would be greatly appreciate.
EDIT
I appreciate the good suggestions, but they did not solve the issue. I did, however, determine that for some reason, it does not like the line:
Set objFolder = objFS.GetFolder(strLOG_PATH & g_strAppName)
If I remove that, as the part to delete old files, it works. I'd like to have that in there, to prevent hundreds of logs files being piled up on servers everywhere.
END EDIT
It could be failing for permissions issues. I would do the following, not necessarily in this order:
- Throw ProcMon on the problem. It will quickly tell you if you are even trying to write. It will also point out permissions issues.
- Start VB6 in Administrator mode if you are on Vista or Win 7.
I figured out what it was. After getting it partially fixed, I was able to log out errors when that bad line failed. It was giving a type mismatch error, which I tracked down to it having multiple definitions for the folder object. The fixed code looks like this:
Dim objFolder As Scripting.Folder
That resolved it. Thanks for all input!
精彩评论