在未处理的异常处理程序中异步写入文件
Writing file asynchronously in unhandled exception handler
我正在为未处理的异常事件编写处理程序,我们会将异常转储到文件中,然后发送到服务器进行进一步分析。现在我想将异常保存到文件中并在下一个 运行 中显示。从处理程序中我调用了一个函数,它有这样的实现:
public async Task WriteDataAsync(string filename, string data)
{
var file = await this.storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, data);
}
问题是,如果文件不存在,它会被创建,但异常数据不会保存到文件中。但是,如果文件存在,数据将被正确保存。此函数在未用于处理未处理的异常时有效。
在这件事上我有两个问题。
- 在那种情况下出现这种行为的原因是什么。我想这与 async/await 方法的工作方式有关,这在正常情况下是可以的,但在这种情况下处理异常时会出现问题。
在这种情况下是否有其他异步写入文件的解决方案?我有一个可行的解决方案,即在这种情况下使 运行 同步运行。
public void WriteDataAsync(string filename, string data)
{
var fileTask = this.storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting).AsTask();
var writingToFileTask = FileIO.WriteTextAsync(fileTask.Result, data).AsTask();
writingToFileTask.Wait();
}
我也想过先创建文件再写。但是,数据没有再次写入文件。
我想你可以使用 FileMode.OpenOrCreate:
public async Task WriteDataAsync(string fileName, string data)
{
byte[] text = Encoding.Unicode.GetBytes(data);
using (var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
{
await stream.WriteAsync(text, 0, text.Length);
};
}
What is the reason for such behavior in that context. I guess it has to do with way the async/await methods works.
你猜对了。当我们调用 await
方法时,它 return 将控制权交给异步方法的调用者。在此上下文中,控件将最终 return 到 Windows 运行时。通常在触发 UnhandledException
事件后,Windows 运行时将终止应用程序。因此,应用程序有可能在异步操作完成其工作之前终止。这可能会导致您所看到的行为。
Are there other solutions to write to file asynchronously in that context? I have one working solution, which is to make run function synchronously in this case.
对于这种场景,通常我们需要像SuspendingDeferral or BackgroundTaskDeferral. However there is no such thing in UnhandledException
event. Your solution is correct in this case, we can make these operations synchronously to avoid this issue. You can refer to this blog: Strategies for Handling Errors in your Windows Store Apps这样的异步操作的延迟来处理未处理的异常。它也使用这个解决方案。
我正在为未处理的异常事件编写处理程序,我们会将异常转储到文件中,然后发送到服务器进行进一步分析。现在我想将异常保存到文件中并在下一个 运行 中显示。从处理程序中我调用了一个函数,它有这样的实现:
public async Task WriteDataAsync(string filename, string data)
{
var file = await this.storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, data);
}
问题是,如果文件不存在,它会被创建,但异常数据不会保存到文件中。但是,如果文件存在,数据将被正确保存。此函数在未用于处理未处理的异常时有效。
在这件事上我有两个问题。
- 在那种情况下出现这种行为的原因是什么。我想这与 async/await 方法的工作方式有关,这在正常情况下是可以的,但在这种情况下处理异常时会出现问题。
在这种情况下是否有其他异步写入文件的解决方案?我有一个可行的解决方案,即在这种情况下使 运行 同步运行。
public void WriteDataAsync(string filename, string data) { var fileTask = this.storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting).AsTask(); var writingToFileTask = FileIO.WriteTextAsync(fileTask.Result, data).AsTask(); writingToFileTask.Wait(); }
我也想过先创建文件再写。但是,数据没有再次写入文件。
我想你可以使用 FileMode.OpenOrCreate:
public async Task WriteDataAsync(string fileName, string data)
{
byte[] text = Encoding.Unicode.GetBytes(data);
using (var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
{
await stream.WriteAsync(text, 0, text.Length);
};
}
What is the reason for such behavior in that context. I guess it has to do with way the async/await methods works.
你猜对了。当我们调用 await
方法时,它 return 将控制权交给异步方法的调用者。在此上下文中,控件将最终 return 到 Windows 运行时。通常在触发 UnhandledException
事件后,Windows 运行时将终止应用程序。因此,应用程序有可能在异步操作完成其工作之前终止。这可能会导致您所看到的行为。
Are there other solutions to write to file asynchronously in that context? I have one working solution, which is to make run function synchronously in this case.
对于这种场景,通常我们需要像SuspendingDeferral or BackgroundTaskDeferral. However there is no such thing in UnhandledException
event. Your solution is correct in this case, we can make these operations synchronously to avoid this issue. You can refer to this blog: Strategies for Handling Errors in your Windows Store Apps这样的异步操作的延迟来处理未处理的异常。它也使用这个解决方案。