【发布时间】:2015-10-13 10:02:22
【问题描述】:
我已经阅读了这些资源(first Stack Overflow question、second Stack Overflow question、third Stack Overflow question、fourth Stack Overflow question 和 Microsoft streamwriter),但并没有解决问题。
我在下面有这段代码。
第一
Private Sub WritePropertyLog(ByVal folderPath As String, ByVal file_name As String, ByVal property_cd As String, _
ByVal property_name As String, ByVal status As String)
Using w As StreamWriter = File.AppendText("Files\" + "DS_IKO_" + Date.Today.ToString("yyyyMMdd") + ".log")
PropertyLog(folderPath, file_name, property_cd, property_name, status, w)
End Using
End Sub
Private Sub PropertyLog(ByVal folderPath As String, ByVal file_name As String, ByVal property_cd As String, _
ByVal property_name As String, ByVal status As String, ByVal w As TextWriter)
w.Write(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + ", ")
w.WriteLine("{0}, {1}, {2}, {3}, {4}", folderPath, file_name, property_cd, property_name, status)
End Sub
第二次
' Logging each record ----------
Private Sub WriteRecordLog(ByVal row_number As String, ByVal columnB As String, ByVal columnC As String, _
ByVal columnD As String, ByVal sekisan_cd As String, ByVal propertyCd As String, ByVal filename As String)
Dim strFile As String = "Files\" + propertyCd = "_" + filename + "_" + Date.Now.ToString("yyyyMMdd") + ".log"
Using writer As StreamWriter = File.AppendText(strFile)
RecordLog(row_number, columnB, columnC, columnD, sekisan_cd, writer)
End Using
End Sub
Private Sub RecordLog(ByVal row_number As String, ByVal columnB As String, ByVal columnC As String, _
ByVal columnD As String, ByVal sekisan_cd As String, ByVal w As TextWriter)
w.WriteLine("{0}, {1}, {2}, {3}, {4}", row_number, columnB, columnC, columnD, sekisan_cd)
End Sub
我正在尝试创建一个日志文件,如您所见,除了变量之外,两者之间没有太大区别。 FIRST 代码实际上输出或写入.log,但 SECOND 代码不会。我需要这两个代码来编写不同的日志,第二个代码首先执行并且比第一个代码执行的次数更多。
可能是什么问题?
我尝试在第二个代码中添加这个:
Dim fs As FileStream = Nothing
If (Not File.Exists(strFile)) Then
fs = File.Create(strFile)
End If
我什至尝试了.flush() 和.close() 以及另一种方法:
Using
End Using
但没有任何效果。
【问题讨论】:
-
你有异常吗?你用过调试器吗?
-
一般情况下,使用
Path.Combine构建路径。它使它更容易和更具可读性。 -
您确定您的文件路径吗?您是否在正确的文件夹中查找?您正在使用 relative 路径,这意味着在调试期间您正在写入
bin/debug/Files -
propertyCd = "_"看起来不对 - 这是故意的吗? -
使用
String.Format创建文件名而不是连接字符串以避免拼写错误
标签: .net vb.net logging streamwriter