错误日志指出哪个 user/machine 导致了错误

Error log stating which user/machine caused the error

在我的 vb.net 项目中,我实现了它以在 Try...Catch 语句中发现异常时创建错误日志。目前,错误日志说明了错误的 date/time、错误发生的形式和行以及错误的类型。我希望它显示的最后一件事是登录到发生错误的计算机的用户(例如,如果是学校网络使用它,它可以说 student/teacher 已登录)。

这是我的代码:

   Public Shared Sub errorLog(ByVal errormessage, ByVal errorsource)

    Dim strFile As String = "errorLog-" & Today.ToString("dd-MM-yyyy") & ".txt"
    Dim fileExists As Boolean = File.Exists(strFile)
    Using sw As New StreamWriter(File.Open(strFile, FileMode.Append))
        sw.WriteLine( _
            IIf(fileExists, _
                 DateTime.Now & " " & errormessage & vbNewLine & errorsource & vbNewLine, _
            "Error log date: " & Today.ToString("dd-MM-yyyy") & vbNewLine & DateTime.Now & " " & errormessage & vbNewLine & errorsource & vbNewLine))
    End Using
End Sub

当前错误日志示例(table 名称不正确):

Error log date: 15-07-2016 15/07/2016 11:19:31 Invalid object name 'tabe_one'. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) at SQLServerApplication.frmViewDtb.getPeople() in C:\Project Location\Project Folder\Project Subfolder\formName.vb:line number

您可以使用 Environment.UserName 和 Environment.Machine 名称来获取此信息。