简体   繁体   English

Asp.net MVC3 C#,StreamWriter错误

[英]Asp.net MVC3 C#, StreamWriter error

I'm using StreamWriter to write log file. 我正在使用StreamWriter写入日志文件。

And this is the writing log method, 这是写日志方法,

public void Write(string fileNamePrefix, string msg){
    GenerateFileName(fileNamePrefix);

    string logTime = "> "+DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString()+"\r\n";

    using (StreamWriter sw = new StreamWriter(fileName, true))
    {
    sw.WriteLine(logTime + msg);
    sw.Flush();
    sw.Close();
    sw.Dispose();
    }
}

While using that method to write log, I got a error message that say " The process cannot access the file 'C:\\Users\\mark\\documents\\visual studio 2010\\Projects\\eee\\eee.WebUI\\log\\Order_20120613' because it is being used by another process. " 使用该方法写入日志时,出现一条错误消息,提示“ 该进程无法访问文件'C:\\ Users \\ mark \\ documents \\ visual studio 2010 \\ Projects \\ eee \\ eee.WebUI \\ log \\ Order_20120613',因为它正在被另一个进程使用。

Not all the time the error message occur, when I run many process at the same time and the processes are using that method to write log, then I get that error message. 当我同时运行多个进程并且进程正在使用该方法写入日志时,并非总是出现该错误消息,然后我得到了该错误消息。

anybody knows what I am doing wrong? 有人知道我在做什么错吗? how to avoid this error? 如何避免这个错误?

and is that right way for writing log? 那是写日志的正确方法吗? How do you write process log usually? 您通常如何编写过程日志?

Thank you. 谢谢。

I/O failures are expected behavior for I/O related classes. I / O故障是与I / O相关的类的预期行为 So prepare to handle them. 因此,准备好处理它们。

In you particular case it is most likely caused by file name collisions - make sure names for "log" files are unique between processes and threads in each process. 在您的特定情况下,它很可能是由文件名冲突引起的-确保“ log”文件的名称在进程和每个进程中的线程之间是唯一的。

Unrelated comments: 不相关的评论:

  • There is absolutely no need to call sw.Flush();sw.Close(); sw.Dispose(); 绝对不需要调用sw.Flush();sw.Close(); sw.Dispose(); sw.Flush();sw.Close(); sw.Dispose(); if you are using using (since it is already correctly calls sw.Dispose() including exception cases). 如果正在使用using (因为它已经正确调用了sw.Dispose()包括异常情况)。 Even if you would not do that calling both Close and Dispose is of no value as both just call the same code. 即使您不这样做,同时调用Close和Dispose也没有任何意义,因为它们都只是调用相同的代码。

  • Inventing (usually called "reinventing the wheel") log writing and tracing libraries is traditional developers' past time. 发明(通常称为“重新发明轮子”)日志编写和跟踪库是传统开发人员的过去时间。 Unless you consider it learning exercise or entertainment please use existing libraries like one that is already part of .Net framework (which lets you configure multiple trace listeners and so on) or other ones like Log4Net. 除非您认为它是学习运动或娱乐,否则请使用现有的库,例如已经是.Net框架一部分的库(可让您配置多个跟踪侦听器等)或其他诸如Log4Net的库。

You should look into log4net . 您应该查看log4net Thread-safe (which is the problem you're running into here, multiple threads/processes trying to use the same file) and very flexible. 线程安全(这是您在这里遇到的问题,多个线程/进程试图使用同一文件)并且非常灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM