VB6 - 如果达到至少 5mb,如何创建另一个日志文件

问题描述 投票:0回答:1

当日志文件达到 5mb 时,应用程序将挂起。我只想在达到一定大小时创建另一个日志文件或增量日志文件并写入该文件。您能否提供一些我可以使用的示例或功能。以下是我当前的功能并计划使用 FileLen 来检查文件大小。

TIA。

Public Sub LogTraces(ByVal folderName As String, ByVal sLocation As String, _
                               ByVal message As String)
                               
Dim intN As Integer
    
        intN = FreeFile(1)
        Open sLocation & "\" & UCase(folderName) & "\" & UCase(folderName) & "_LOG_" & Format(Now, "MMDDYYYY") & ".log" For Append Shared As #intN
'        Open sLocation & "\LOGS\" & ConnectionID & "-" & Format(Date, "yyyymmdd") & ".log" For Append Shared As #intN

            Print #intN, Format(Now, "[mmddyyyy HH:mm:ss]") & ": " & message
        
        Close #intN
   
End Sub
vb6
1个回答
0
投票

我通过使用函数来计算文件名来调整你的代码。

Option Explicit

Public Sub LogTraces(ByVal folderName As String, ByVal sLocation As String, ByVal message As String)
   Dim fileName As String
   Dim fn As Integer
   
   fileName = GetFileName(folderName, sLocation)
   fn = FreeFile(1)
   Open fileName For Append Shared As #fn
   Print #fn, Format(Now, "[mmddyyyy HH:mm:ss]") & ": " & message
   Close #fn
End Sub

Public Function GetFileName(ByVal folderName As String, ByVal sLocation As String) As String
   Dim i As Integer
   Dim fn As Integer
   
   GetFileName = sLocation & "\" & UCase(folderName) & "\" & UCase(folderName) & "_LOG_" & Format(Now, "MMDDYYYY") & ".log"
   
   'if the file is too big (pick some file size in bytes)
   Do While FileLen(GetFileName) > 5242880
      'get new file name by appending a number
      i = i + 1
      GetFileName = sLocation & "\" & UCase(folderName) & "\" & UCase(folderName) & "_LOG_" & Format(Now, "MMDDYYYY") & "_" & i & ".log"
      
      'create new file if needed
      If Dir(GetFileName) = "" Then
         fn = FreeFile(1)
         Open GetFileName For Output As #fn
         Close #fn
      End If
   Loop
End Function
© www.soinside.com 2019 - 2024. All rights reserved.