无法将日志附加到文件。 [英] Not able to append the logs to a file.

查看:86
本文介绍了无法将日志附加到文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi People,



我正在使用以下语法创建一个附加文件创建日期和时间的文件。



Hi People,

I am creating a file which is appended with the date and time of the creation of file using following syntax.

Public mydate As String = System.DateTime.Now.ToString("yyyyMMddHHmmssfff")










Public fileLoc_logs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "AllSerialLogs.txt".Replace(".txt", "_" & mydate & ".txt"))






上述语法在项目的.vb文件之一(我们说SelectTestType.vb)中声明。



在IOBoardTest.vb中,这是另一个文件,我使用以下语法将日志附加到此文件,



The above syntax is declared in one of the .vb file (Let us say SelectTestType.vb) of the project.

And in IOBoardTest.vb, which is another file, i am appending the logs to this file using following syntax,

Using sw As New System.IO.StreamWriter(SelectTestType.fileLoc_logs, True)   

         &尝试NBSP;  
            sw.WriteLine(STR) 
       赶上例外情况               
Console.WriteLine(" Error {0}",ex.Message) 
       结束尝试
        sw.Close()
结束使用

         Try                sw.WriteLine(str)          Catch ex As Exception                Console.WriteLine("Error {0}", ex.Message)          End Try         sw.Close() End Using




$
但事情是每次都是创建了新日志,生成了一个新文件。



我想将日志存储到第一次创建的文件中。需要帮助。



But the thing is for every time the new logs are created, a new file got generated.

I want to store the logs to a file which is created for the first time. Need help.

推荐答案

您好,

遇到这样的问题时,您需要进行简单测试并编写代码是可维护的,无论它是在类还是表单中。

When having issues like this you need to do simple test and also write code that is maintainable no matter if it's in a class or form.

这是一个简单的类来尝试

Here is a simple class to try

Public Class FileLogOperations
    Public Property FileName As String
    ''' <summary>
    ''' True if last operation had a runtime exception
    ''' </summary>
    ''' <returns></returns>
    Public Property HasException As Boolean
    ''' <summary>
    ''' Exception of last operation ran that throw an exception
    ''' </summary>
    ''' <returns></returns>
    Public Property Exception As Exception
    Public Sub New(ByVal pFileName As String)
        FileName = pFileName
    End Sub
    Public Sub New()
    End Sub
    ''' <summary>
    ''' Write a line with date formatted in front
    ''' </summary>
    ''' <param name="pLine"></param>
    ''' <returns></returns>
    Public Function Write(ByVal pLine As String) As Boolean
        HasException = False
        Try
            Using sw As New IO.StreamWriter(FileName, IO.File.Exists(FileName))
                sw.WriteLine(


" {Now.ToString(" yyyyMMddHHmmssfff")}:{pLine}")
结束使用
Catch ex As Exception
HasException = True
Exception = ex
End Try
Return Not HasException
结束函数
结束类
"{Now.ToString("yyyyMMddHHmmssfff")} : {pLine}") End Using Catch ex As Exception HasException = True Exception = ex End Try Return Not HasException End Function End Class

为其提供测试数据

Public Class Form1
    Private Property ops As New FileLogOperations
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not ops.Write(TextBox1.Text) Then
            MessageBox.Show(


" {ops.Exception.Message}")
End if
End Sub
Private Sub Button2_Click(sender As Object,e As EventArgs)处理Button2.Click
如果不是ops.Write(TextBox2.Text)那么
MessageBox.Show(
"{ops.Exception.Message}") End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If Not ops.Write(TextBox2.Text) Then MessageBox.Show(


这篇关于无法将日志附加到文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆