如果多用户需要同时更新文件,有两个办法:1、为每个请求创建一个单独的文件 2、把文件绑定到另一个对象并使用锁定。
第一种方法:
1、创建唯一的文件名
private string GetFileName() { // Create a unique filename. string fileName = @"Log\user." + Guid.NewGuid().ToString() + ".txt"; // Put the file in the current web application path. return Path.Combine(Request.PhysicalApplicationPath, fileName); }
在Log目录下会生成一个这样的文件:user.20de4e7b-056a-47f0-b40d-579a12f4053a.txt
2、写文件。
FileStream fs = new FileStream(fileName, FileMode, FileAccess, FileShare);
StreamWriter w = new StreamWriter(fs);
如果是第一次,FileMode.Create, 否则, FileMode.Append。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void Log(string message)
{
// Check for the file.
FileMode mode;
if (ViewState["LogFile"] == null)
{
// First, create a unique user-specific file name.
ViewState["LogFile"] = GetFileName();
// The log file must be created.
mode = FileMode.Create;
}
else
{
// Add to the existing file.
mode = FileMode.Append;
}
// Write the message.
// A using block ensures the file is automatically closed,
// even in the case of error.
string fileName = (string)ViewState["LogFile"];
using (FileStream fs = new FileStream(fileName, mode))
{
StreamWriter w = new StreamWriter(fs);
w.WriteLine(DateTime.Now);
w.WriteLine(message);
w.WriteLine();
w.Close();
}
}
3、每次加在页面时进行检查,并读取和删除文件。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Log("Page loaded for the first time.");
}
else
{
Log("Page posted back.");
}
}
protected void cmdRead_Click(object sender, EventArgs e)
{
StringBuilder log = new StringBuilder();
string fileName = (string)ViewState["LogFile"];
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
StreamReader r = new StreamReader(fs);
// Read line by line (allows you to add
// line breaks to the web page).
string line;
do
{
line = r.ReadLine();
if (line != null)
{
log.Append(line + "<br />");
}
} while (line != null);
r.Close();
}
lblInfo.Text = log.ToString();
}
protected void cmdDelete_Click(object sender, EventArgs e)
{
if (ViewState["LogFile"] != null)
{
File.Delete((string)ViewState["LogFile"]);
ViewState["LogFile"] = null;
}
}
4、asp文件内容如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<body>
<form id="form1" runat="server">
<div>
<asp:Button id="Button1" runat="server"
Width="90px" Text="Post Back"></asp:Button>
<asp:Button id="cmdRead" runat="server"
Width="90px" Text="Read Log" OnClick="cmdRead_Click"></asp:Button>
<asp:Button id="cmdDelete" runat="server"
Width="90px" Text="Delete Log" OnClick="cmdDelete_Click"></asp:Button>
<br /><br />
<div style="padding: 10px;border-style: dotted;border-width: 1px;background-color: #FFFFCC;">
<asp:Label id="lblInfo" runat="server"></asp:Label>
</div>
</div>
</form>
</body>
第2中方法,如果多个用户需要更新同一个文件。两个方法,一是创建一个Logger类的全局实例,在该类中使用lock方法。另一个是响应global.asax文件中的HttpApplication.Start事件。
但是这种方法存在问题,不允许每个用户同时读写同一文件的片段。此外,文件被某个客户端锁定时,其他请求不得不等待。所以,ASP.NET应用程序一般不使用基于文件的日志,而是把日志写在Windows事件日志或数据库中。