日志文件锁定在C#中的问题 [英] Log File Locking Issue in C#

查看:155
本文介绍了日志文件锁定在C#中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个windows服务,把日志文件写入一个XML日志文件。在服务运行时,我维护日志文件的句柄,并在服务停止时关闭,刷新并处理它。文件写入操作仅限于服务,并且我将文件流在FileAccess.ReadWrite中打开,而共享设置为FileShare.Read。我希望能够通过另一个应用程序的XmlRead()调用来打开和查看此文件,但出现错误,指出该文件正在被另一个进程使用。我已经阅读了另一篇文章,这是可能的印象:其他主题

正在使用的作者是刷新,关闭,并处置,并且每个写入的文件流都被刷新。这在.Net中是不可能的,或者我可能做错了什么?这个代码的简化版本如下:
$ b $ pre $ if(_logFS == null)
_logFS = new FileStream(_fileName, FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.Read);

if(!initFile)
{
_logFS.Seek(-13,SeekOrigin.End);
}

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using(XmlWriter writer = XmlWriter.Create(_logFS,settings))
{
if(initFile)
{
writer.WriteRaw(<?xml version = \1.0 \encoding = \utf-8 \standalone = \yes\?> \\\
);
writer.WriteStartElement(Entries,http://www.abcdefg.com);
}

writer.WriteStartElement(Exception);

writer.WriteEndElement();


writer.Flush();
writer.Close();
}

_logFS.Flush();

如下所示:

  _LogDS = new XmlLogFile(); 
using(FileStream logFS = new FileStream(_fileName,FileMode。 Open,FileAccess.Read)
{
_LogDS.ReadXml(logFS);
}


<您还需要关闭FileStream。至少,您需要在服务退出时关闭它,或者当FileStream将不在应用程序的作用域中时关闭它。解决方案



您应该能够以另一种方式从另一个应用程序中以ReadOnly的方式打开它,但是您必须指定它不是默认值。



在你的服务中您需要启用文件共享:

  FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite ,FileShare.Read); 

而在您的阅读器应用程序中:

  FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.ReadWrite); 

如果没有 FileShare.Read 打开文件阅读失败。任何其他请求打开文件进行写入的应用程序仍然会失败,对于使用 FileShare.ReadWrite 的写入启用共享。 FileShare 的默认选项是 None


I have a windows service that writes out log file entries to an XML log file. I maintain a handle to the log file while the service is operational, and close, flush and dispose of it when the service is stopped. The file write operations are by the service only, and I have the filestream open in FileAccess.ReadWrite while sharing is set to FileShare.Read. I would like to be able to open and view this file with an XmlRead() call by another application, but I get an error stating the file is being used by another process. I had read another post on this and was under the impression this was possible: Other Thread.

The writer in use is flushed, closed, and disposed of, and each write the filestream is flushed. Is this just not possible in .Net, or have I perhaps done something wrong? A cutdown version of the code follows:

if (_logFS == null)
        _logFS = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

if (!initFile)
{
    _logFS.Seek(-13, SeekOrigin.End);
}

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create(_logFS, settings))
{
    if (initFile)
    {
        writer.WriteRaw("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n");
        writer.WriteStartElement("Entries", "http://www.abcdefg.com);
    }

    writer.WriteStartElement("Exception");
    // write out some stuff here.
    writer.WriteEndElement();


    writer.Flush();
    writer.Close();
}

_logFS.Flush();

The file opening code is now as follows:

_LogDS = new XmlLogFile();
using (FileStream logFS = new FileStream(_fileName, FileMode.Open, FileAccess.Read)
{
    _LogDS.ReadXml(logFS);
}

解决方案

You also need to close the FileStream. At a minimum, you need to close it when your service exits, or when the FileStream would go out of the application's scope.

You should be able to open it as ReadOnly from another application either way, but you have to specify that, it's not a default.

In your service you need to enable the file sharing:

FileStream fs = new FileStream("path", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

And in your reader application:

FileStream fs = new FileStream("path", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

Without the FileShare.Read, all requests to open the file for reading fail. Any other application requesting to open the file for writing will still fail, for write-enabled sharing you'd use FileShare.ReadWrite. The default option for FileShare is None.

这篇关于日志文件锁定在C#中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆