通过网络服务写入文件

发布于 2024-09-05 14:46:16 字数 160 浏览 9 评论 0原文

我有一个 wevservice,我想将日志写入文本文件。

我的问题是我不知道创建流编写器时要给出什么路径:

TextWriter tw = new StreamWriter("????");

您能帮助我应该输入什么路径吗?

I have a wevservice, and I would like to write logs into a textfile.

My problem is that i do not know what path to give when creating the streamwriter:

TextWriter tw = new StreamWriter("????");

Can you please help what path I should enter?

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

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

发布评论

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

评论(2

世态炎凉 2024-09-12 14:46:16

将其放在哪里并不重要,您只需为 Web 服务授予您要写入的位置的适当权限即可。您可以查看应用程序池以了解需要向哪个用户授予权限,或者您可以使用模拟。

如果您使用 "MyLogfile.log" 它将位于与 Web 服务相同的位置,因此相对路径会将其置于相对于该位置的位置。不过,您也可以使用绝对路径,例如 "c:/log/MyLogfile.log"。

我希望它有帮助。

It doesn't matter where you put it, you just need to give the web service the appropriate permissions to the location you want to write to. You can take a look at the Application pool to see which user you need to give the permissions, or you can use impersonation.

If you use "MyLogfile.log" it will be located in the same location as the web service, so a relative path will put it relative to that location. You can however use a absolute path too, like "c:/log/MyLogfile.log".

I hope it helped.

﹉夏雨初晴づ 2024-09-12 14:46:16

请参阅 Server.MapPath这篇关于 Codeproject 的文章

更新:这是一个示例,以便部署在服务器上并为日志文件创建一个子目录。您可以使用浏览器进行测试。

<%@ WebService Language="c#" Class="Soap"%>
using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

[WebService]
public class Soap : System.Web.Services.WebService
{
    [WebMethod(EnableSession=true)]
    public bool Login(string userName, string password)
    {
        //NOTE: There are better ways of doing authentication. This is just illustrates Session usage.
        LogText("Login User = " + userName);
        UserName = userName;
        return true;
    }

    [WebMethod(EnableSession=true)]
    public void Logout()
    {    
        LogText("Logout User = " + UserName);
        Context.Session.Abandon();
    }

    private string UserName {
        get {return (string)Context.Session["User"];}
        set {Context.Session["User"] = value;}
    }

    private void LogText(string s) {
        string fname = Path.Combine(
            Server.MapPath( "/logs" ), "logfile.txt");
        TextWriter tw = new StreamWriter(fname);
        tw.Write("Yada yada :" + s);
        tw.Close();
    }
}

See Server.MapPath and this article on Codeproject

Update: Here's an example is in order deploy on server and create a subdirectory for log files. You can test using your browser.

<%@ WebService Language="c#" Class="Soap"%>
using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

[WebService]
public class Soap : System.Web.Services.WebService
{
    [WebMethod(EnableSession=true)]
    public bool Login(string userName, string password)
    {
        //NOTE: There are better ways of doing authentication. This is just illustrates Session usage.
        LogText("Login User = " + userName);
        UserName = userName;
        return true;
    }

    [WebMethod(EnableSession=true)]
    public void Logout()
    {    
        LogText("Logout User = " + UserName);
        Context.Session.Abandon();
    }

    private string UserName {
        get {return (string)Context.Session["User"];}
        set {Context.Session["User"] = value;}
    }

    private void LogText(string s) {
        string fname = Path.Combine(
            Server.MapPath( "/logs" ), "logfile.txt");
        TextWriter tw = new StreamWriter(fname);
        tw.Write("Yada yada :" + s);
        tw.Close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文