我正在尝试以不同的方法访问相同的日志文件,但出现错误

本文关键字:文件 日志 错误 方法 访问 | 更新日期: 2023-09-27 18:31:44

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

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后,我得到"当前文件已关闭"。

我正在尝试以不同的方法访问相同的日志文件,但出现错误

首先你有

using (StreamWriter w ...

然后里面你有

`foreach`  loop

里面有

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

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

using是你的处置机制。你只需要里面有一个标志来验证块是否完成得很好。

您可以使用

StreamWriter来写入文件。写入文件后,释放文件。

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

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