我正在尝试将文本写入.txt文件,但它不起作用

时间:2021-11-20 13:19:20

I'm trying to write some info to a text file when there is an error in the application. I added this code to the Application_Error method in global.asax but it still dont work:

当应用程序出错时,我正在尝试将一些信息写入文本文件。我将此代码添加到global.asax中的Application_Error方法,但它仍然不起作用:

    void Application_Error(object sender, EventArgs e)
{
    string path = Server.MapPath("Error.txt");
    Exception ex = Server.GetLastError();
    if (!File.Exists(path))
    {
        File.Create(path);
    }
    if (File.Exists(path))
    {
        TextWriter tw = new StreamWriter(path, true);
        tw.WriteLine("{0} : An Error Has Occurred, Error Description",DateTime.Now.ToString());
        tw.WriteLine(@"{");
        tw.WriteLine("Error Message: {0}", ex.Message);
        tw.WriteLine("Source: {0}", ex.Source);
        if (ex.StackTrace != null) tw.WriteLine("StackTrace: {0}", ex.StackTrace);
        tw.WriteLine(@"}");
        tw.Close();
    }
}

If it matters, i'm also redirecting to an error page when there is an error, here is the web.config file:

如果重要,我还会在出现错误时重定向到错误页面,这里是web.config文件:

<customErrors mode="On" defaultRedirect="ASPX/Error.aspx" redirectMode="ResponseRedirect">
  <error statusCode="404" redirect="ASPX/Error404.aspx"/>
</customErrors>

So do you have any idea what is wrong with my code? how can I make it write the text into the file?

所以你知道我的代码有什么问题吗?如何将文本写入文件?


Edit: I just needed to run vs as administrator, the probelm is solved

编辑:我只需要以管理员身份运行vs,问题就解决了

3 个解决方案

#1


One problem with your code is that you don't have a using block for your File.Create call. The problem is that this method creates a file and returns a stream. The stream will most likely still be locking the file when you try to write to it.

您的代码的一个问题是您没有File.Create调用的使用块。问题是此方法创建一个文件并返回一个流。当您尝试写入文件时,流很可能仍会锁定文件。

To resolve this, you can use an empty using block that will close and dispose of the stream, like so:

要解决此问题,您可以使用一个空的使用块来关闭和处理流,如下所示:

if (!File.Exists(path))
{
    using (File.Create(path)) { }
}

A related problem that may not always rear it's head is that you are not disposing of your TextWriter. You should also wrap the code that uses that in a using block, to ensure it's taken care of (and you can remove the call to .Close since that will happen automatically):

一个相关的问题可能并不总是让人头疼的是你没有处理你的TextWriter。您还应该在使用块中包装使用它的代码,以确保它被处理(并且您可以删除对.Close的调用,因为这将自动发生):

using (TextWriter tw = new StreamWriter(path, true))
{
    tw.WriteLine("{0} : An Error Has Occurred, Error Description", 
        DateTime.Now.ToString());
    tw.WriteLine(@"{");
    tw.WriteLine("Error Message: {0}", ex.Message);
    tw.WriteLine("Source: {0}", ex.Source);
    if (ex.StackTrace != null) tw.WriteLine("StackTrace: {0}", ex.StackTrace);
    tw.WriteLine(@"}");
}

#2


Your problem could be due to the use of Server.MapPath.

您的问题可能是由于使用了Server.MapPath。

Try changing:

string path = Server.MapPath("Error.txt");

To something like:

对于这样的事情:

string path = String.Format("{0}\\{1}", HttpRuntime.AppDomainAppPath, "Error.txt");

#3


I just had to run visual studio as administrator, dont waste time trying to add more answers\comments, this problem is solved

我只是以管理员身份运行visual studio,不要浪费时间尝试添加更多答案\评论,这个问题就解决了

#1


One problem with your code is that you don't have a using block for your File.Create call. The problem is that this method creates a file and returns a stream. The stream will most likely still be locking the file when you try to write to it.

您的代码的一个问题是您没有File.Create调用的使用块。问题是此方法创建一个文件并返回一个流。当您尝试写入文件时,流很可能仍会锁定文件。

To resolve this, you can use an empty using block that will close and dispose of the stream, like so:

要解决此问题,您可以使用一个空的使用块来关闭和处理流,如下所示:

if (!File.Exists(path))
{
    using (File.Create(path)) { }
}

A related problem that may not always rear it's head is that you are not disposing of your TextWriter. You should also wrap the code that uses that in a using block, to ensure it's taken care of (and you can remove the call to .Close since that will happen automatically):

一个相关的问题可能并不总是让人头疼的是你没有处理你的TextWriter。您还应该在使用块中包装使用它的代码,以确保它被处理(并且您可以删除对.Close的调用,因为这将自动发生):

using (TextWriter tw = new StreamWriter(path, true))
{
    tw.WriteLine("{0} : An Error Has Occurred, Error Description", 
        DateTime.Now.ToString());
    tw.WriteLine(@"{");
    tw.WriteLine("Error Message: {0}", ex.Message);
    tw.WriteLine("Source: {0}", ex.Source);
    if (ex.StackTrace != null) tw.WriteLine("StackTrace: {0}", ex.StackTrace);
    tw.WriteLine(@"}");
}

#2


Your problem could be due to the use of Server.MapPath.

您的问题可能是由于使用了Server.MapPath。

Try changing:

string path = Server.MapPath("Error.txt");

To something like:

对于这样的事情:

string path = String.Format("{0}\\{1}", HttpRuntime.AppDomainAppPath, "Error.txt");

#3


I just had to run visual studio as administrator, dont waste time trying to add more answers\comments, this problem is solved

我只是以管理员身份运行visual studio,不要浪费时间尝试添加更多答案\评论,这个问题就解决了