0

It might be a little funny but I am facing some stupid issue.

I am using

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

and

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

when I using this the error is given to me is "Could not find a part of the path "

If am storing the path in a string then its working fine else producing error.

For your reference I am copying my function code snippet.

Please help

Thanks

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

如果您在函数代码片段中使用注释掉的行更改当前行,那么您的逻辑就会改变,因为当您使用 Path 时,会在路径字符串中添加一个文件名 [dd-MMM-yy].log。但是当您直接使用 ConfigurationManager.AppSettings("LogFilePath1") 时,您不会使用该添加!

于 2013-08-19T10:16:26.117 回答
0

我假设您没有使用带有斜杠的文件夹路径(例如:@“C:\FolderName\”),这就是您收到错误的原因。

于 2013-08-19T10:38:29.673 回答