0

少しおかしいかもしれませんが、私は愚かな問題に直面しています。

私は使っている

File.Create(ConfigurationManager.AppSettings("LogFilePath1")).Dispose()

Using writer As StreamWriter = File.AppendText(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1")))

これを使用すると、「パスの一部が見つかりませんでした」というエラーが表示されます

パスを文字列に保存している場合は、正常に機能し、そうでない場合はエラーが発生します。

ご参考までに、関数のコード スニペットをコピーします。

助けてください

ありがとう

Public Shared Function LogErrorToLogFile(ByVal ee As Exception, ByVal userFriendlyError As String) As String
        Try
               Dim path As String = context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1"))
            ' check if directory exists
            If Not Directory.Exists(path) Then
                'Directory.CreateDirectory(path)
                Directory.CreateDirectory(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1")))
            End If
            path = path & DateTime.Today.ToString("dd-MMM-yy") & ".log"
            ' check if file exist
            If Not File.Exists(path) Then
                File.Create(path).Dispose()
                'File.Create(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1"))).Dispose()
                File.Create(ConfigurationManager.AppSettings("LogFilePath1")).Dispose()
            End If
            ' log the error now
            Using writer As StreamWriter = File.AppendText(path)
                'Using writer As StreamWriter = File.AppendText(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1")))
              writer.WriteLine(HttpUtility.HtmlEncode(vbCr & vbLf & "Log written at : " & DateTime.Now.ToString() & vbCr & vbLf & "Error occured on page : " & context.Current.Request.Url.ToString() & vbCr & vbLf & vbCr & vbLf & "Here is the actual error :" & vbLf & ee.ToString()))
                writer.WriteLine("==========================================")
                writer.Flush()
                writer.Close()
            End Using
            Return userFriendlyError
        Catch
            Throw
        End Try
    End Function
4

2 に答える 2

1

関数コード スニペットでコメントアウトされた行を使用して現在の行を変更すると、パスを使用するとファイル名 [dd-MMM-yy].log がパス文字列に追加されるため、ロジックが変更されます。ただし、 ConfigurationManager.AppSettings("LogFilePath1") を直接使用する場合は、その追加を使用しません!

于 2013-08-19T10:16:26.117 に答える