如何在服务器 ASP.net 上存储文件

发布于 2024-07-26 08:12:13 字数 951 浏览 3 评论 0原文

好的,我正在尝试为在 ASP.net 和 C# 上运行的网站提供错误日志文件。

我尝试使用常规路径存储驱动。 期待它,因为它是 C# 代码(页面后面的代码),它将获取服务器并将其存储在那里。 (最后它是在服务器上执行的代码)

不幸的是对我来说这不是真的。 它保存在客户端打开站点的本地计算机上。

如何强制将其保存在服务器计算机上?

我希望所有错误日志都集中在一处。 有关阅读或如何执行此操作的任何建议。

   string path = "C:\\WebSite";
   string error = "error";          
   try
    {
      // check if dir exists if it does i add file name to the path
      // i removed code to keep it simple
      path += "\\logs.txt";

      FileStream log_fs = 
              new FileStream(path, FileMode.Append, FileAccess.Write);
      StreamWriter log_sw = 
              new StreamWriter(log_fs);
      string log = String.Empty;

      // formate log string using error message

      log_sw.Write(log);
      log_sw.Close();
      log_fs.Close();
   }
   catch (Exception ex)
   {
        // here I e-mail the error in case logging failed
   }

此代码将在本地计算机而不是服务器上生成文件

Ok so I am trying to have error log file for the website running on ASP.net and C#.

I tried storing to drive with regular path.
Expecting it because it is in c# code (code behind page) it will pick up the server and store it there. (In the end it is a code that gets executed on server)

Unfortunatelly for me thats not true. It gets saved on local machine from which client opens the site.

How can you force it to be saved on server machine?

I want all of the error logs on one place.
Any suggestions on reading or how to do this.

   string path = "C:\\WebSite";
   string error = "error";          
   try
    {
      // check if dir exists if it does i add file name to the path
      // i removed code to keep it simple
      path += "\\logs.txt";

      FileStream log_fs = 
              new FileStream(path, FileMode.Append, FileAccess.Write);
      StreamWriter log_sw = 
              new StreamWriter(log_fs);
      string log = String.Empty;

      // formate log string using error message

      log_sw.Write(log);
      log_sw.Close();
      log_fs.Close();
   }
   catch (Exception ex)
   {
        // here I e-mail the error in case logging failed
   }

This code will generate file on local machine, instead of server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小耗子 2024-08-02 08:12:13

您可以使用如下代码登录到 Web 服务器上的任何位置(ASPNET 用户帐户有权写入):

string log = String.Empty;

// format log string using error message

try
{
    using (StreamWriter logFile = new StreamWriter(@"C:\WebSite\logs.txt", true))
    {
        logFile.WriteLine(log);
    }
}
catch
{
    // here I e-mail the error in case logging failed
}

这会将文件写入托管网站的 Web 服务器。 现在,如果您在开发过程中在本地运行网站,并且使用内置的 VS Web 服务器或 IIS,那么在本地测试网站时,当然会在您的计算机上创建该文件。

You can log to any location on the web server (that the ASPNET user account has permission to write to) with code such as this:

string log = String.Empty;

// format log string using error message

try
{
    using (StreamWriter logFile = new StreamWriter(@"C:\WebSite\logs.txt", true))
    {
        logFile.WriteLine(log);
    }
}
catch
{
    // here I e-mail the error in case logging failed
}

This will write the file to the web server hosting the website. Now, if you're running the website locally during development and you're using either the built-in VS webserver or IIS, then of course the file will be created on your machine when testing the website locally.

苦行僧 2024-08-02 08:12:13

要回答问题的建议部分,您是否考虑过使用 ELMAH,它会记录错误到不同的存储:

Microsoft SQL Server
Oracle(Oracle错误日志)
SQLite(版本 3)数据库文件
Microsoft Access(访问错误日志)
VistaDB(VistaDB错误日志)
松散的 XML 文件
RAM(内存中)

或通过电子邮件发送,twitter

elmah 非常容易设置且非常有效

To answer the suggestion portion of your question, have you considered using ELMAH, it will log errors to different storages:

Microsoft SQL Server
Oracle (OracleErrorLog)
SQLite (version 3) database file
Microsoft Access (AccessErrorLog)
VistaDB (VistaDBErrorLog)
Loose XML files
RAM (in-memory)

or send via email, twitter

elmah is very easy to setup and very effective

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文