in my vb.net project, I have implemented it to create an error log when an exception is found in a Try...Catch
statement. Currently, the error log states the date/time of the error, the form and line the error occurred on and what type of error it is. The final thing I want it to show is the user who was signed in to the computer that the error occurred on (eg so that if it's a school network using it, it can say the student/teacher that was logged in).
This my code for it:
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
An example of a current error log (incorrect table name):
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
You can use Environment.UserName and Environment.Machine name to get this information.