I have a C#/XAML application and I use ETW to log events to flat file.
9/10 times the StorageFile is not set and my event is not logged.
Constructor:
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);
}
The files are created always but 9/10 times are not written to.
For reference here is how I set up the listener:
EventListener informationListener = new AppEventsListener("Information");
informationListener.EnableEvents(AppEventsSource.Log, EventLevel.Informational);
And sample log:
AppEventsSource.Log.Info("log entry #1");