我有一个C#/ XAML应用程序,我使用ETW将事件记录到平面文件。
未设置StorageFile的9/10倍且未记录我的事件。
构造
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");