4

I want to programmatically get the event log that appears in a folder in eventvwr. This one is for Windows 8 Apps: Microsoft-Windows-AppHost/Admin. I'm running as Admin.

This is not working:

System.Diagnostics.EventLog.SourceExists("Microsoft-Windows-AppHost/Admin");

This is not working either:

EventLogQuery queryMicrosoftWindowsAppHost = new EventLogQuery("Microsoft-Windows-AppHost/Admin", PathType.LogName);
_eventsMicrosoftWindowsAppHost = new List<EventRecordWrittenEventArgs>();
_eventLogWatcherMicrosoftWindowsAppHost = new EventLogWatcher(queryMicrosoftWindowsAppHost);
_eventLogWatcherMicrosoftWindowsAppHost.EventRecordWritten += (object sender, EventRecordWrittenEventArgs e) =>
{
    _eventsMicrosoftWindowsAppHost.Add(e);
};
_eventLogWatcherMicrosoftWindowsAppHost.Enabled = true;
John Koerner
  • 37,428
  • 8
  • 84
  • 134
will
  • 3,975
  • 6
  • 33
  • 48
  • are you trying to do this from a Windows Store app (which is likely going to work) or from a desktop app? What does "not working" mean? exceptions? – Jim O'Neil Dec 29 '12 at 15:20
  • I'm trying to query the event logs from OUTSIDE the Appx, with a regular old C# console app. I'm not getting any errors, just that the event log is not found. Is the syntax for querying the event log correct? i.e. "Microsoft-Windows-AppHost/Admin". – will Dec 31 '12 at 03:20

1 Answers1

2

You can read the event log using this code:

EventLogReader reader = new EventLogReader("Microsoft-Windows-AppHost/Admin");
var evt = reader.ReadEvent();
while (evt!= null)
{
    // Write the message to the console
    Console.WriteLine(evt.FormatDescription());
    evt = reader.ReadEvent();
}

If you want to attach your handler, your code is close, but you can simply pass the string into the watcher, instead of using the query object:

var watcher = new EventLogWatcher("Microsoft-Windows-AppHost/Admin");
watcher.EventRecordWritten += (object s, EventRecordWrittenEventArgs e1) =>
    {
        Console.WriteLine(e1.EventRecord.FormatDescription());
    };
watcher.Enabled = true;
John Koerner
  • 37,428
  • 8
  • 84
  • 134