在 vbscript 中写入单个日志文件 [英] Write to a single log file in vbscript

查看:32
本文介绍了在 vbscript 中写入单个日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小子来创建日志文件

I have written a small sub to create log file

sub WriteLogFileLine(sLogFileNameFull, sLogFileLine) 
    logfolder = "C:\Users\TEMPPAHIR\LearnVB\Logfolder\"
    ScriptName1 = Replace(Wscript.ScriptName, ".vbs", "")
    sLogFileName = ScriptName1 & "_" & date & "_" & hour(now) & "-" & minute(now) & "-" & second(now) & "_log.txt"
    sLogFileNameFull = logfolder & sLogFileName
    dateStamp = Now()
    Set MyLog = objFSO.OpenTextFile(sLogFileNameFull, 8, True)
    MyLog.WriteLine(cstr(dateStamp) + vbTab + "-" + vbTab + sLogFileLine) 
    MyLog.Close
    Set MyLog = Nothing 
end sub

我在主 vb 脚本函数中多次调用此子函数,以这种方式将消息写入日志文件,

I am calling this sub multiple times in my main vb script function to write messages to the log file this way,

'******************************************************************************
'* Main Script Body
'******************************************************************************
Function DoAllWork
    On Error Resume Next
    Dim sFile, FromDate, ToDate, FromLocation, MyFile, objFolder, Filename, objArgs, FilenameReq, line, inFile, outFile, outputLines
    Dim FromdateFile, FromdateFileReq, lineDt, CFileDt, CopyFiles, line1, CFile, resultX, stringY, resultY, resultfinal, outputLine
    Dim FromFolderList, ToLocationArg, outputLines1, Arch, ToLocationArg1, writeOutput
    Const ForReading = 1, ForWriting = 2, ForAppending = 8 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objArgs = WScript.Arguments 
    Set MyLog = objFSO.OpenTextFile(sLogFileNameFull, 8, True)

    If WScript.Arguments.Count = 4 Then 
        Call WriteLogFileLine(sLogFileNameFull, "Total number of arguments passed are " & WScript.Arguments.Count & " and they are as follows : ")
        'Parameter1, begin with index0
        FromDate = WScript.Arguments(0)
        'Parameter2
        ToDate = WScript.Arguments(1)
        'Parameter3
        FromLocationArg = WScript.Arguments(2)
        'Parameter4
        ToLocation = WScript.Arguments(3)
    Else 
        Call WriteLogFileLine(sLogFileNameFull, "Error, Must pass 4 arguments to the script !" & vbcrlf)
        Wscript.quit 
    End if 
    Call WriteLogFileLine(sLogFileNameFull, "First Argument, the date folder FROM where files needs to be copied is : " & FromDate & vbcrlf)
    Call WriteLogFileLine(sLogFileNameFull, "Second Argument, the date folder TILL where files needs to be copied is : " & ToDate & vbcrlf)
MyLog.Close
    Set MyLog = Nothing 

    DoAllWork = Err.Number
    Call WriteLogFileLine(sLogFileNameFull, "End of the script !" & vbcrlf)
End Function

'******************************************************************************
'* End of the script.
'******************************************************************************

但是在执行脚本时有时会生成多个日志文件,我猜每次时间戳更改时它都会创建一个新的日志文件并在该文件中写入日志.

But while executing the script sometimes there are multiple log files getting generated, I guess everytime the timestamp is changing it is creating a new log file and writting log in that file.

QUE :我希望将所有日志写入一个日志文件,该文件的时间戳在我的脚本开始时获取.请帮忙!!

QUE : I want all the log to be written to one single log file whose timestamp is picked up at the beginning of my script. Please help !!

推荐答案

你的日志函数有一个日志文件完整路径的参数,所以你应该在代码的开头定义一次文件名,并在整个过程中使用该名称整个脚本(我建议对 FileSystemObject 实例做同样的事情).从日志记录功能中删除所有定义文件名的代码.

Your logging function has an argument for the full path to the log file, so you should define the filename once at the beginning of your code and use that name throughout the entire script (I'd recommend doing the same for the FileSystemObject instance). Remove all code defining the filename from the logging function.

Set objFSO = CreateObject("Scripting.FileSystemObject")

logfolder    = "C:\Users\TEMPPAHIR\LearnVB\Logfolder"
ScriptName1  = Replace(Wscript.ScriptName, ".vbs", "")
sLogFileName = ScriptName1 & "_" & Date & "_" & Hour(Now) & "-" & _
               Minute(Now) & "-" & Second(Now) & "_log.txt"
logfile = objFSO.BuildPath(logfolder, sLogFileName)

Sub WriteLogFileLine(sLogFileNameFull, sLogFileLine)
    Set MyLog = objFSO.OpenTextFile(sLogFileNameFull, 8, True)
    MyLog.WriteLine Now & vbTab & "-" & vbTab & sLogFileLine
    MyLog.Close
End Sub

...
WriteLogFileLine logfile, "some log line"
...

您可能想看看我写了几年的记录器类以前(如果你能原谅这个无耻的插件).它可能有助于简化您的日志处理.将类代码复制到您的脚本中,您可以像这样进行日志记录:

You may want to take a look at the logger class I wrote a couple years ago (if you'll forgive the shameless plug). It might help simplify your log handling. Copy the class code into your script and you can do logging like this:

logfolder = "C:\Users\TEMPPAHIR\LearnVB\Logfolder"
logname   = Replace(Wscript.ScriptName, ".vbs", "") & "_" & Date & "_" & _
            Hour(Now) & "-" & Minute(Now) & "-" & Second(Now) & "_log.txt"
logfile   = objFSO.BuildPath(logfolder, logname)

Set clog = New CLogger
clog.LogToConsole = False
clog.LogFile = logfile      'open log
clog.IncludeTimestamp = True

...
clog.LogInfo "some message"
clog.LogError "other message"
...

clog.LogFile = ""           'close log

当脚本终止时,日志文件会自动关闭(如果它之前没有在代码中关闭).

The log file is closed automatically when the script terminates (if it wasn't already closed in the code before).

这篇关于在 vbscript 中写入单个日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆