要编写登录过程的日志文件

时间:2019-05-20 06:16:51

标签: vb.net

在我输入正确的凭据的同时,登录时客户端计算机上也出现错误,因此,我想创建登录名的日志文件,但无法执行此操作。

Dim myFile As System.IO.File
Dim fWriter As System.IO.StreamWriter
Dim fName As String, fLog As String

'string to be written in the text file
fLog = Now.ToString("hh:mm tt") & vbTab & "LOGINFO" & vbTab & "userLogin"

'file path and name

fName = "C:\Program Files\TTG\RAPID\LOGIN_ERROR_LOG_" & Now.ToString("ddMMyyyy") & ".txt"

'if file is existing already: the boolean parameter is set to true to append a new line on the file

If myFile.Exists(fName) Then
    fWriter = New System.IO.StreamWriter(fName, True)
Else

'if it does not exist, create a new text
    fWriter = myFile.CreateText(repFName)
End If

fWriter.WriteLine (fLog)
'fWriter.Close()

Close fWriter
fWriter = Nothing

在上面的代码中,我得到第一行,第二行和第九行的“未定义用户定义类型”,

注意-在第九行中,当我尝试使用“新建Keyowrd”时。 请帮助我。

非常感谢。

1 个答案:

答案 0 :(得分:0)

我专门使用File类。您不声明File类的实例,因为方法是静态的(在vb.net中共享)

我更改了日期字符串的格式,因此它将在文件资源管理器中按日期顺序排列。更容易找到您要查找的文件。

我本以为您也想知道用户名。如果是这样,请将其添加到您的flog中。登录表单上可能有一个TextBox,您可以在其中获取它。

如果遇到访问冲突,请将目录更改为“文档”中的内容。

您将需要在文件顶部添加引用。 Imports System.IO

    Dim fLog = Now.ToString("hh:mm tt") & vbTab & "LOGINFO" & vbTab & "userLogin"
    Dim fName = "C:\Program Files\TTG\RAPID\LOGIN_ERROR_LOG_" & Now.ToString("yyyy-MM-dd") & ".txt"
    If File.Exists(fName) Then
        File.AppendAllText(fName, fLog)
    Else
        File.WriteAllText(fName, fLog)
    End If