C#/XAML アプリケーションがあり、ETW を使用してイベントをフラット ファイルに記録します。
9/10 回、StorageFile が設定されておらず、イベントがログに記録されません。
コンストラクタ:
public AppEventSource(string logFileName)
{
this._logFileName = logFileName;
AssignLocalFile();
}
private async void AssignLocalFile()
{
_storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
_logFileName),
CreationCollisonOption.OpenIfExists);
}
private async void WriteToFile(IEnumerable<string> lines)
{
await _semaphore.WaitAsync();
await Task.Run(async() =>
{
await FileIO.AppendLinesAsync(_storageFile, lines);
_semaphore.Release();
});
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if(_storageFile == null) return;
List<string> lines = new List<string>();
// formatting stuff....
WriteToFile(lines);
}
ファイルは常に作成されますが、9/10 回は書き込まれません。
参考までに、リスナーの設定方法を次に示します。
EventListener informationListener = new AppEventsListener("Information");
informationListener.EnableEvents(AppEventsSource.Log, EventLevel.Informational);
サンプルログ:
AppEventsSource.Log.Info("log entry #1");