I am writing an XPath query string to select records from the Windows Event Log where the Event source contains a particular string. I have a version which performs an exact match where the Path element of the query is
Path='Application'>*[System[Provider[@Name='MyApp']
This works fine, but I want to find all events where the source contains 'MyApp'. I have read various articles and I think it should be
Path='Application'>*[System[Provider[contains(@Name, 'MyApp']
but this fails with an 'Invalid Query' error.
Any idea what I am doing wrong?
Update:
I edited my query in line with the fix below and my full query string is now:
var Query = @"<QueryList><Query Id='0' Path='Application'>
<Select Path='Application'>*[System[Provider[contains(@Name, 'MyApp')] and (Level=2 or Level=3)]]</Select>
</Query></QueryList>";
var EventLogQuery = new EventLogQuery("Application", PathType.LogName, Query);
but this still fails. If I change the query to be
var Query = @"<QueryList><Query Id='0' Path='Application'>
<Select Path='Application'>*[System[Provider[@Name='MyApp'] and (Level=2 or Level=3)]]</Select>
</Query></QueryList>";
it all works.
Does the EventLogQuery support 'contains'. This article How to select for content contains substring in Windows Event Viewer using XPath 1.0? suggests it does not.