Web服务日志文件问题 [英] web service log file issue
问题描述
我使用以下方法在我的网络服务中记录一些有用的信息:
I am using the following method to log some useful info in my web service:
private void logRequest(string strMessage)
     {
           随机ra = new Random();
            string REQUEST_LOG =" c:\\test\\" + ra.Next()+" .xml"; StreamWriter sr = File.Exists(REQUEST_LOG)? File.AppendText(REQUEST_LOG):File.CreateText(REQUEST_LOG);
sr.WriteLine(strMessage);
            sr.Close();
       我在我的Web服务中调用了这个方法三次,它应该随机生成三个不同的文件。但不幸的是,它只生成一个文件,我尝试登录的所有信息都在同一个文件中。为什么随机函数不能在这里生成三个不同的文件?
private void logRequest(string strMessage)
{
Random ra = new Random();
string REQUEST_LOG = "c:\\test\\" + ra.Next() + ".xml";
StreamWriter sr = File.Exists(REQUEST_LOG) ? File.AppendText(REQUEST_LOG) : File.CreateText(REQUEST_LOG);
sr.WriteLine(strMessage);
sr.Close();
}
I call this method three times in my web service, it supposed to randomly generate three different files. but unfortunately, it only generate one file, all the info i try to log are in this same file. why the random function not working here to generate three different files?
推荐答案
这可能是因为你每次创建一个新的Random实例方法被调用 - 你使用了本地!
另外,总是使用使用块:
This might be because you're creating a new instance of Random every time this method is called - you used a local!
Also, always use a using block:
using (StreamWriter sr = File.Exists(REQUEST_LOG) ?
File.AppendText(REQUEST_LOG) :
File.CreateText(REQUEST_LOG))
{
sr.WriteLine(strMessage);
}
这篇关于Web服务日志文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!