0

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

我的问题是我不知道在创建 streamwriter 时要给出什么路径:

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

你能帮我输入什么路径吗?

4

2 回答 2

3

不管你把它放在哪里,你只需要给 Web 服务适当的权限到你想写的位置。您可以查看应用程序池以查看需要授予哪个用户权限,或者您可以使用模拟。

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

我希望它有所帮助。

于 2010-06-12T20:13:40.067 回答
1

请参阅Server.MapPathCodeproject 上的这篇文章

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

<%@ 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();
    }
}
于 2010-06-12T20:04:46.943 回答