将日志写入txt文件中

Sub 将信息写入日志中(日志内容)
    '判断日志文件夹是否存在,不存在就创建
    If Not Directory.Exists(Application.StartupPath & "\log") Then
        Directory.CreateDirectory(Application.StartupPath & "\log")
    End If
    '新建一个以当前时间命名的日志文件,精确到秒
    Dim logFilePath As String = Application.StartupPath & "\log\" & DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") & ".txt"
    '将指定内容写入日志文件
    Using sw As StreamWriter = File.AppendText(logFilePath)
        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & " " & 日志内容)
    End Using
End Sub
posted @ 2024-12-06 14:41  令勾子  阅读(26)  评论(0)    收藏  举报