2013-01-16 105 views
0

試圖寫有FileStream.BeginWrite()方法的XML文件,但它給了我一個無法訪問關閉文件錯誤錯誤無法訪問關閉文件

我的代碼是

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)函數給我上面的錯誤。

+0

本文應該對您有所幫助 - http://www.codeproject.com/Articles/15278/How-to-Append-to-a-Large-XML-File – dash

回答

2

您已經在using()聲明中打開了該流,但是您在該聲明中執行的最後一件事是發出異步寫入。那麼,只要發出異步寫入,using語句就會終止,並且您的文件將很快關閉,很可能在異步寫入之前發生。不要在這裏做異步寫入。除非在異步寫入完成後保持文件處於打開狀態並有一個處理程序將其關閉,否則它不會帶來任何好處。

1

你讓你的using語句走出去的範圍由在該行踢的後臺線程:

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

如果你想這個函數是線程安全的,你可能要考慮線程鎖定WriteXmlLog方法。

相關問題