Payload exception -- cannot serialize the payload index was outside the bounds of the array.

Topics: Semantic Logging Application Block
Aug 23, 2013 at 6:39 PM
I'm trying to use the Semantic Logging block and everything is working except my payload keeps coming through as:

Payload : [Exception : Cannot serialize the payload: Index was outside the bound
s of the array.] [Exception : Cannot serialize the payload: Index was outside th
e bounds of the array.]

Here is my logging code:
public class MainLogger : EventSource
        [Event(5, Level = EventLevel.Error, Message = "Test: {0}")]
        public void Starting()
            this.WriteEvent(5, "Hello");

static void Main(string[] args)
            ObservableEventListener listener = new ObservableEventListener();
            MainLogger logger = new MainLogger();
            listener.EnableEvents(logger, System.Diagnostics.Tracing.EventLevel.Informational);
            listener.LogToConsole(new EventTextFormatter(), new DefaultConsoleColorMapper());
            listener.LogToRollingFlatFile(@"C:\temp\logging", 1000, "yyyymmdd", Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollFileExistsBehavior.Increment, Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollInterval.Day);


What am I missing?


Aug 25, 2013 at 9:14 PM
The issue is that the Starting() method does not have any parameters while the call to WriteEvent contains a string argument. The arguments passed to an EventSource method (not annotated by the NonEventAttribute) must match the arguments passed to the WriteEvent method. From EventAttribute Class documentation:
When you apply this attribute to an ETW event method defined on an EventSource-derived class, you must call the WriteEvent method on the base class, passing the event ID, followed by the same arguments as the defined method is passed.
So that is the reason for the error but how to prevent these runtime errors from occurring? The best way is to use the EventSourceAnalyzer to validate the EventSource. So for example:
catch (Exception e)
Would give the following information:
The number of WriteEvent arguments and event parameters are different in event name 'Starting'.
Which is considerably more meaningful than the rather cryptic PayloadException message.

You could use the EventSourceAnalyzer as a unit test for your EventSource to catch these types of errors before encountering them at runtime.

Randy Levy
Enterprise Library support engineer
Support How-to