VB.NET 写日志文件

时间:2022-06-01 14:03:15
''' <summary>
''' 写日志
''' </summary>
''' <param name="msg">日志内容</param>
''' <param name="context">上下文对象</param>
''' <remarks></remarks>
Sub WriteLog(msg As String, Optional context As System.Web.HttpContext = Nothing)
Dim filepath As String = ""
If context Is Nothing Then
filepath = Me.LogFilePath
Else
filepath = context.Server.MapPath("..") + "\File\LogFile\"
End If
filepath &= Now.ToString("yyyyMMdd") & ".txt"
Dim content As String
If context IsNot Nothing Then
content = "<-------------异常时间:" & Now.ToString("yyyy-MM-dd HH:mm:ss") & _
Chr(13) & msg & "来源:" & context.Request.Url.ToString & _
"错误信息:" & context.Error.Message & _
Chr(13) & "错误详情:" & Chr(13) & context.Error.StackTrace & Chr(13) & _
"--------------->" & Chr(13)
Else
content = "异常时间:" & Now.ToString("yyyy-MM-dd HH:mm:ss") + Chr(13) & msg & Chr(13)
End If
File.AppendAllText(filepath, content)
End Sub