Cannot get EventRecord.FormatDescription(IEnumerable<Object>) to work

284 Views Asked by At

I am trying to call FormatDescription(ienumerable) of an EventRecord but cannot get it to replace the strings. What am I doing wrong?

    public void StartLogReading()
    {          
        EventLogQuery evtLogQuery = new EventLogQuery(
            "Application",
            PathType.LogName,
            "*[System/EventRecordID >= 0]");

        var watcher = new EventLogWatcher(evtLogQuery, null, true);
        watcher.EventRecordWritten += EventLogEventRead;
        watcher.Enabled = true;

    }

    private void EventLogEventRead(object sender, EventRecordWrittenEventArgs e)
    {
        List<object> aParams = new List<object>();
        foreach (EventProperty eventProperty in e.EventRecord.Properties)
        {
            aParams.Add(eventProperty.Value);
        }
        string description = e.EventRecord.FormatDescription(aParams);
        // Do stuff with description
    }

The string description always contains the event message without the params being replaced.

I am actually trying to use this technique as a workaround for the exception I get sometimes: "The description string for parameter reference (%1) could not be found" when calling e.EventRecord.FormatDescription().

0

There are 0 best solutions below