vb.net 如何将 Catch 异常记录到文本文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24017860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to log Catch exception to a text file
提问by Shmewnix
I have a vb.net program that runs a Try Catch. What i'd like to have happen, if there is an exception is have it log that exception to a text file. I have:
我有一个运行 Try Catch 的 vb.net 程序。我想要发生的是,如果有异常是让它将该异常记录到文本文件中。我有:
Catch ex As Exception
MsgBox(ex.Message)
My.Application.Log.WriteException(ex, TraceEventType.Error, "Exception " & "with argument " & "C:\Log.txt" & ".")
End Try
But it's not sending the exception to the logfile. What am I missing?
但它不会将异常发送到日志文件。我错过了什么?
回答by ForkandBeard
I think the example here: http://msdn.microsoft.com/en-us/library/dsxzceby(v=vs.90).aspxmight have mislead you a bit. I find it a bit odd.
我认为这里的例子:http: //msdn.microsoft.com/en-us/library/dsxzceby(v=vs.90) .aspx可能有点误导你。我觉得有点奇怪。
I think this is probably better:
我认为这可能更好:
Public Sub SomeMethodWhichMightGenerateException(byval someArg as String)
Try
' Code that might generate an exception goes here.
' For example:
' Dim x As Object
' MsgBox(x.ToString)
Catch ex As Exception
My.Application.Log.WriteException(ex, _
TraceEventType.Error, _
"Exception with argument " & someArg & ".")
End Try
End Sub
fileNameis just a random variable in the msdn example and nothing to to do with where the Exceptionis logged...
fileName只是 msdn 示例中的一个随机变量,与Exception记录的位置无关...
Look here http://msdn.microsoft.com/en-us/library/7fx0fexe(v=vs.90).aspxto see where My.Application.Log.WriteExceptionwrites data to.
在此处查看http://msdn.microsoft.com/en-us/library/7fx0fexe(v=vs.90).aspx以查看My.Application.Log.WriteException将数据写入的位置。
Alternatively you could do something like this:
或者,您可以执行以下操作:
Catch ex As Exception
IO.File.AppendAllText("C:\Log.txt", String.Format("{0}{1}", Environment.NewLine, ex.ToString()))
End Try
As others have said, however, you're probably better off using an existing error logging framework.
但是,正如其他人所说,您最好使用现有的错误日志记录框架。
回答by sanket parikh
Public Function logerrors(ByVal [error] As String)
Dim filename As String = "Log_" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt"
Dim filepath As String = HttpContext.Current.Server.MapPath(Convert.ToString("~/ErrorLog/") & filename)
If File.Exists(filepath) Then
Using stwriter As New StreamWriter(filepath, True)
stwriter.WriteLine("-------------------START-------------" + DateTime.Now)
stwriter.WriteLine(Convert.ToString("Page :"))
stwriter.WriteLine([error])
stwriter.WriteLine("-------------------END-------------" + DateTime.Now)
End Using
Else
Dim stwriter As StreamWriter = File.CreateText(filepath)
stwriter.WriteLine("-------------------START-------------" + DateTime.Now)
stwriter.WriteLine(Convert.ToString("Page :"))
stwriter.WriteLine([error])
stwriter.WriteLine("-------------------END-------------" + DateTime.Now)
stwriter.Close()
End If
Return [error]
End Function
回答by pingle60
Adding a timestamp in the log file
在日志文件中添加时间戳
Catch ex As Exception
IO.File.AppendAllText("C:\Log.txt", String.Format("{0}[{1}]{2}", Environment.NewLine, DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss"), ex.ToString()))
End Try

