0

尝试使用 FileStream.BeginWrite() 方法写入 xml 文件,但它给了我一个Can Not Access the Close File错误

我的代码是

public void WriteXmlLog(string logType, string logFlag, string logModule, string logLocation, string logText, string logStackTrace)
{
    if (!File.Exists(_logFilePath))
    {
        File.WriteAllText(_logFilePath, "<?xml version='1.0' encoding='utf-8' standalone='yes'?>\r\n<AppXmlLogWritter></AppXmlLogWritter>");
    }

    XmlDocument xmlDoc = new XmlDocument();

    using (fileStream = new FileStream(_logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        xmlDoc.Load(fileStream);
        XmlElement newelement = xmlDoc.CreateElement("LogData");
        XmlElement xmlLogID = xmlDoc.CreateElement("LogID");
        XmlElement xmlLogDateTime = xmlDoc.CreateElement("LogDateTime");
        XmlElement xmlLogType = xmlDoc.CreateElement("LogType");
        XmlElement xmlLogFlag = xmlDoc.CreateElement("LogFlag");
        XmlElement xmlLogApplication = xmlDoc.CreateElement("LogApplication");
        XmlElement xmlLogModule = xmlDoc.CreateElement("LogModule");
        XmlElement xmlLogLocation = xmlDoc.CreateElement("LogLocation");
        XmlElement xmlLogText = xmlDoc.CreateElement("LogText");
        XmlElement xmlLogStackTrace = xmlDoc.CreateElement("LogStackTrace");

        xmlLogID.InnerText = _logIDPrefix + currentDateTime + randomNumber;
        xmlLogDateTime.InnerText = currentDateTime;
        xmlLogType.InnerText = ((LogTypes)Convert.ToInt32(logType)).ToString();
        xmlLogFlag.InnerText = logFlag;
        xmlLogApplication.InnerText = _logApplication;
        xmlLogModule.InnerText = logModule;
        xmlLogLocation.InnerText = logLocation;
        xmlLogText.InnerText = logText;
        xmlLogStackTrace.InnerText = logStackTrace;

        newelement.AppendChild(xmlLogID);
        newelement.AppendChild(xmlLogDateTime);
        newelement.AppendChild(xmlLogType);
        newelement.AppendChild(xmlLogFlag);
        newelement.AppendChild(xmlLogApplication);
        newelement.AppendChild(xmlLogModule);
        newelement.AppendChild(xmlLogLocation);
        newelement.AppendChild(xmlLogText);

        xmlDoc.DocumentElement.AppendChild(newelement);
        Byte[] myByteArray = Encoding.UTF8.GetBytes(newelement.ToString());
        fileStream.BeginWrite(myByteArray, 0, myByteArray.Length, WriteAsyncCallback, new LogWritter(myByteArray, fileStream));
    }

    xmlDoc.Save(_logFilePath);
}

public Stream MyStream { get; set; }

private void WriteAsyncCallback(IAsyncResult ar)
{
    LogWritter info = ar.AsyncState as LogWritter;
    info.MyStream.EndWrite(ar);
}

WriteAsyncCallback(IAsyncResult ar)函数给了我上面的错误。

4

2 回答 2

2

您已经在语句中打开了流using(),但您在该语句中做的最后一件事是发出异步写入。好吧,一旦您发出异步写入,using 语句将终止并且您的文件将关闭,很可能在异步写入发生之前。不要在这里进行异步写入。除非您保持文件打开并在异步写入完成后有处理程序将其关闭,否则它不会为您带来任何好处。

于 2013-01-16T13:50:10.927 回答
1

您通过在此行启动后台线程来让您的 using 语句超出范围:

fileStream.BeginWrite(myByteArray, 0, myByteArray.Length, WriteAsyncCallback, new LogWritter(myByteArray, fileStream));

如果您希望此函数是线程安全的,您可能需要考虑线程锁定 WriteXmlLog 方法。

于 2013-01-16T14:56:30.503 回答