VB6:无法使用我的 ActiveX dll 进行任何日志记录
我正在尝试向似乎卡住运行的 dll 添加日志记录。我尝试简单地添加几个子例程来记录到文件:
Sub PrepareLogging(ByVal strAppName As String)
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
代码将运行,但它似乎只是忽略所有这些日志记录功能。我知道代码是正确的,因为它可以在其他 dll 和 exe 中工作,我在其中添加了带有这些子例程的模块。我尝试添加一个基本命令来直接记录到事件日志:
App.LogEvent strMsg
但是失败了。如果我尝试使用 Goto ErrorHandler 部分捕获代码中的错误:
ErrorHandler:
App.LogEvent Err.Number & Err.Description
应用程序完全失败,这样我在登录 Web 服务后甚至无法选择任何选项。我知道问题不在于应用程序日志已满,因为它继续添加新事件。任何关于造成这种情况的建议或想法将不胜感激。
编辑
我很欣赏这些好的建议,但它们没有解决问题。然而,我确实确定,由于某种原因,它不喜欢这一行:
Set objFolder = objFS.GetFolder(strLOG_PATH & g_strAppName)
如果我删除它,作为删除旧文件的部分,它就会起作用。我希望将其放在那里,以防止数百个日志文件堆积在各地的服务器上。
结束编辑
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)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能会因权限问题而失败。我会执行以下操作,不一定按此顺序:
It could be failing for permissions issues. I would do the following, not necessarily in this order:
我知道那是什么了。在部分修复后,当坏线出现故障时,我能够注销错误。它给出了类型不匹配错误,我发现它具有文件夹对象的多个定义。固定代码如下所示:
解决了它。感谢您的所有投入!
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:
That resolved it. Thanks for all input!