【问题标题】:StorageFile read content while file is in use by another processStorageFile 在文件被另一个进程使用时读取内容
【发布时间】:2016-05-26 16:19:56
【问题描述】:
目前我正在使用 MetroLog。它使用文件流将信息记录到日志文件中。
是否有可能在 MetroLog 的文件流使用 StorageFile 时读取其内容
using (var randomAccessStream = await localFile.OpenReadAsync())
{
...
}
此代码将导致Access Denied 异常。该文件存在,并且看起来它具有正确的权限,因为如果我禁用日志记录它就可以工作。
【问题讨论】:
标签:
c#
windows-store-apps
file-storage
【解决方案1】:
我遇到了同样的问题。我的解决方案如下(我的所有代码都放在设备实现中,而不是在 pcl 中):
在 Metrolog 初始化时保存 Streaming 文件目标以供以后使用:
private static StreamingFileTarget _target;
...
LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, _target);
在打开/读取 Metrolog 的当前日志文件之前关闭文件目标的所有打开文件:
await _target.CloseAllOpenFiles();
your code...
当前日志文件将在下一个日志命令时再次打开。