ConfigurationErrorsException - 进程无法访问文件 'c:\eventlog.config',因为它正被另一个进程使用
ConfigurationErrorsException- The process cannot access the file 'c:\eventlog.config' because it is being used by another process
团队,
我有这段代码,它总是在某种竞争条件下将自己捆绑在一起,尤其是当两个或多个资源试图同时写入 eventlog.config 文件时。我已经用谷歌搜索了几个渠道,但无法解决这个错误。谁能帮我修改这段代码,以便我可以消除竞争条件。
private void UpdateLastEventId(IList<EventLogEntry> entries)
{
if (entries.Count > 0)
{
EventLogEntry lastEntry = entries[entries.Count - 1];
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var configSettings = config.AppSettings.Settings;
string key = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", _eventLogFilter.EventLog, _eventLogFilter.MD5Hash);
if (configSettings[key] == null)
{
configSettings.Add(key, lastEntry.Index.ToString(CultureInfo.InvariantCulture));
}
else
{
configSettings[key].Value = lastEntry.Index.ToString(CultureInfo.InvariantCulture);
}
config.Save(ConfigurationSaveMode.Modified);//Error seems to happen here
}
}
private static readonly object _configLogLock = new object();
private void UpdateLastEventId(IList<EventLogEntry> entries)
{
if (entries.Count > 0)
{
EventLogEntry lastEntry = entries[entries.Count - 1];
lock (_configLogLock)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var configSettings = config.AppSettings.Settings;
string key = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", _eventLogFilter.EventLog, _eventLogFilter.MD5Hash);
if (configSettings[key] == null)
{
configSettings.Add(key, lastEntry.Index.ToString(CultureInfo.InvariantCulture));
}
else
{
configSettings[key].Value = lastEntry.Index.ToString(CultureInfo.InvariantCulture);
}
config.Save(ConfigurationSaveMode.Modified);//Error seems to happen here
}
}
}
团队,
我有这段代码,它总是在某种竞争条件下将自己捆绑在一起,尤其是当两个或多个资源试图同时写入 eventlog.config 文件时。我已经用谷歌搜索了几个渠道,但无法解决这个错误。谁能帮我修改这段代码,以便我可以消除竞争条件。
private void UpdateLastEventId(IList<EventLogEntry> entries)
{
if (entries.Count > 0)
{
EventLogEntry lastEntry = entries[entries.Count - 1];
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var configSettings = config.AppSettings.Settings;
string key = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", _eventLogFilter.EventLog, _eventLogFilter.MD5Hash);
if (configSettings[key] == null)
{
configSettings.Add(key, lastEntry.Index.ToString(CultureInfo.InvariantCulture));
}
else
{
configSettings[key].Value = lastEntry.Index.ToString(CultureInfo.InvariantCulture);
}
config.Save(ConfigurationSaveMode.Modified);//Error seems to happen here
}
}
private static readonly object _configLogLock = new object();
private void UpdateLastEventId(IList<EventLogEntry> entries)
{
if (entries.Count > 0)
{
EventLogEntry lastEntry = entries[entries.Count - 1];
lock (_configLogLock)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var configSettings = config.AppSettings.Settings;
string key = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", _eventLogFilter.EventLog, _eventLogFilter.MD5Hash);
if (configSettings[key] == null)
{
configSettings.Add(key, lastEntry.Index.ToString(CultureInfo.InvariantCulture));
}
else
{
configSettings[key].Value = lastEntry.Index.ToString(CultureInfo.InvariantCulture);
}
config.Save(ConfigurationSaveMode.Modified);//Error seems to happen here
}
}
}