0

我正在尝试创建一个日志文件并写入该文件,并且我以不同的方法使用相同的文件,但出现错误。如果我做错了,请纠正我。

foreach (var corporationObj in corporations)
{
    // Log which corporation it is currenlty doing
    _keepItDry.Log("Started Data Summarisation for corporation: @" + corporationObj.Description, w);
    // Pass the coporationId and call method to DoProductStatsForCorporation and also pass the log file to continue our logging.
    w.Flush();
    // w.Close();
    w.Dispose();

    DoProductStatsForCorporation(corporationObj.Id, path);
}

以下是在循环中调用的方法的清单:

private void DoProductStatsForCorporation(int corporationId, string logFilePath)
{
    var corporationManufacturerRepository = new CorporationManufacturerRepository();
    var manufacturerRepository = new ManufacturerRepository();
    // Get brand sfor the coporationId passes
    var corporationManufacturers  = corporationManufacturerRepository.GetAllManufacturersForCorporation(corporationId);
    //check before process is used by another object
    var fileInfo = new FileInfo(logFilePath);
    IsFileLocked(fileInfo);
    using (StreamWriter w = File.AppendText(logFilePath))
    {
        // If brands are existing for the corporation proceed.
        if (corporationManufacturers.Any())
        {
            // loop through each brand to processs
            foreach (var corporationManufacturer in corporationManufacturers)
            {
#region ProductStats
                // get the manufacturer row so that we can know more details about the manufacturer
                var manufacturer =  manufacturerRepository.GetManufacturer(corporationManufacturer.ManufacturerId);
                // Get the countries for the manufacturer

                _keepItDry.Log("Started Doing data summarisation for Brand: @ " + manufacturer.Description, w);
                // this is common method so extracted to reuse it.
                // we need to clos ethe file as we are sending to another function.
                w.Flush();
                w.Dispose();

                DoProductStatsForBrand(manufacturer, logFilePath);

#endregion ProductStats

                _keepItDry.AddTolistBox(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + ": Ended Summarizing data for " + manufacturer.Description, _listBoxLog);
            }
        }
        else
        {
            _keepItDry.Log(" No Brands are there for the selected Corporation Please check if Brands are mapped for this Corporation ? : @ ", w);
            _keepItDry.AddTolistBox(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "  : No Brands are there for the selected Corporation Please check if Brands are mapped for this Corporation ", _listBoxLog);
        }

        //  w.Flush();
        // w.Dispose();
    }    
    //  GC.Collect();
}

来到下一个公司 ID 后,我收到“当前文件已关闭”。

4

2 回答 2

1

首先你有

using (StreamWriter w ...

然后在里面你有

`foreach`  loop

在里面你有

w.Flush();
w.Dispose();

你刚刚处理了你的对象 - 它是如何工作的?如果你有 2 corporationManufacturers,你会去一个循环然后出错......

using是你的处置机制。您只需要在内部设置一个标志来验证该块是否已顺利完成。

于 2013-09-17T03:39:05.400 回答
0

可以StreamWriter用来写文件。写入文件后处理文件。

streamWriter.Flush();
streamWriter.Close();

Close()也调用该Dispose()方法。

于 2013-09-17T05:57:20.740 回答