wincc 用脚本记录日志

时间:2021-04-22 13:59:03

方法1:

在vbs脚本库里面添加

Sub LogTxt(message)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fileSystemObj, fileSpec    ,logFile
    Dim currentDate,currentTime    ,dirName
    dirName =  "d:\log\"      
    currentDate = Year(Date)&"-"& Right("0"&Month(Date),2) &"-" &Right("0"&Day(Date),2)
    currentTime = Time    
    Set fileSystemObj =CreateObject("Scripting.FileSystemObject")    
    If Not fileSystemObj.folderExists(dirName) Then         
        fileSystemObj.CreateFolder(dirName)    
    End If        
    fileSpec = dirName &currentDate& ".txt"
    If Not (fileSystemObj.FileExists(filespec)) Then     
        Set logFile = fileSystemObj.CreateTextFile(fileSpec,ForWriting,True)
        logFile.WriteLine ("#######################################################################")         
        logFile.WriteLine (currentDate &" "& currentTime & " 日志开始记录: "  )         
        logFile.WriteLine ("#######################################################################")         
        logFile.Close       
        Set logFile = Nothing        
    End If      
    Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)    
    logFile.WriteLine (currentDate &","&currentTime & "," & message)    
    logFile.Close    
    Set logFile = Nothing    
    Set fileSystemObj = Nothing    
End Sub

使用时,用下面的代码:
Call LogTxt("失败是成功之母")

方法2:

建立如下vbs脚本,

Sub logtxt(message)
    Dim objShell
    Set objShell = CreateObject("Wscript.Shell")    
    objShell.Run  "logtxt.exe " &message,0    
    '0代表后台运行程序.不会出现窗口.
    'logtxt.exe 建议复制到windows目录.任意调用不会找不到.
    '默认日志目录:D:\运行信息\LogTxt,在logtxt.exe.config文件内可以更改
End Sub

使用时: Call logtxt("失败是成功之母")

下面是命令行日志程序,下载后,放到windows目录,或者其他目录,调用时指定完整路径.

logtxt