gpt4 book ai didi

c# - asp.net 试图为我的网络应用程序创建一个日志文件

转载 作者:行者123 更新时间:2023-11-30 21:08:01 25 4
gpt4 key购买 nike

我正在尝试将一个 log.txt 文件放在我的应用程序的根目录下,并想写入它。但似乎这不起作用。感谢您的帮助。

错误消息(我认为我没有写入正确的文件,顺便说一句,这是本地主机。如果它是本地主机或网络主机,我希望它能工作):

Access to the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\log.txt' is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

这是我的代码:

 public static void LogErrorMessage(Exception e)
{
using (StreamWriter w = File.AppendText("log.txt"))
{
Log(e.ToString(),w);
w.Close();
}

}



private static void Log(String logMessage, TextWriter w)
{
w.Write("\r\nLog Entry : ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", logMessage);
w.WriteLine("-------------------------------");
// Update the underlying file.
w.Flush();
}

最佳答案

我认为你需要:

public static void LogErrorMessage(Exception e, string Directory)
{
using (StreamWriter w = File.AppendText(Directory + "/log.txt"))
{
Log(e.ToString(),w);
w.Close();
}

}

ASPX 类

public void Page_Load()
{
Logger.LogErrorMessage(ex, Server.MapPath("~"));

}

关于c# - asp.net 试图为我的网络应用程序创建一个日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com