网页服务端中,希望记录服务器的异常信息,同时因为是服务端,所以不要显示异常窗口,还要及时给前端一个服务端异常提示,不要让前端直接等待死机。
如果用BeforeShowErrorMessage事件,这时不知道怎么给前端回写服务端异常,如果用
try
Catch ex As Exception
Functions.Execute("错误日志", ex)
e.WriteString("服务端异步异常,请联系管理员!")
End Try
这时的问题是,BeforeShowErrorMessage事件不起作用,只能靠Functions.Execute("错误日志", ex)这个内部函数,但是这样写的异常日志没有“Event: 字符型,返回发生错误的事件名称,如果错误发生在自定义函数中,则返回函数名”,造成错误无法排查,不知道有啥办法能处理。
错误日志内部函数如下:
Dim v As Exception = args(0)
Dim msg As String
If TypeOf v Is Exception Then '如果是Exception的异常类型,则递归获取所有的异常堆栈
Do While v IsNot Nothing
msg &= v.Message & vbcrlf & v.StackTrace & vbcrlf
v = v.InnerException
Loop
End If
'msg = v.Event & ":" & msg
'Functions.Execute("错误日志", msg)
If FileSys.DirectoryExists(ProjectPath & "log") = False Then '如果目录不存在
FileSys.CreateDirectory(ProjectPath & "log")'创建
End If
For Each File As String In FileSys.GetFiles(ProjectPath & "log")
Dim ifo As New FileInfo(File)
If ifo.CreationTime.AddDays(16) < Date.Now Then
FileSys.DeleteFile(File, 2, 2) '则彻底删除之
End If
Next
Output.Logs("AppLogging").Add(Format( Date.Now, "yyyy-MM-dd HH:mm:ss.ffff") & vbCrLf & msg)
Output.Logs("AppLogging").Save(ProjectPath & "log\" & Format(Date.Today, "yyyyMMdd") & "log.txt", True) '日志位于当前项目目录里
Output.Logs("AppLogging").Clear