An ergodic theorem for filtering with applications to stability