---
title: Event batching for the C# SDK
slug: feature-experimentation/docs/event-batching-csharp
docTags: 
createdAt: 2026-08-05T20:39:56.000Z
---

The C# SDK lets you batch events and includes options to set an event batch size, event time interval and event flush interval. The benefit of event batching is less network traffic for the same number of impression and conversion events tracked.

In the C# SDK, `BatchEventProcessor` provides the implementation of the `EventProcessor` interface and batches events. You can control batching based on two parameters:

- **Batch size** – Defines the number of batched events before sending to Optimizely Feature Experimentation.
- **Flush interval** – Defines the amount of time after which any batched events should be sent to Optimizely.

An event consisting of the batched payload is sent when the batch size reaches the specified limit or the flush interval reaches the specified time limit. `BatchEventProcessor` options are described in the following sections.

:::hint{type="info"}
**Note**
Event batching works with both out-of-the-box and custom event dispatchers.

The event batching process does not remove any personally identifiable information (PII) from events. You must still ensure that you are not sending any unnecessary PII to Optimizely.
:::

## Basic example

```csharp
using OptimizelySDK;

public static class Program
{
   public static void Main(string[] args)
    {
        var sdkKey = args[0];
        
        // Returns Optimizely Client
        OptimizelyFactory.NewDefaultInstance(sdkKey);
    }
}
```

By default, the batch size is 10 and the flush interval is 30 seconds.

## Advanced example

```csharp
using OptimizelySDK;
using OptimizelySDK.Config;
using OptimizelySDK.Event;

public static class Program
{
    public static void Main(string[] args)
    {
        var sdkKey = args[0];
        
        var projectConfigManager = HttpProjectConfigManager.Builder()
            .WithSdkKey(sdkKey)
            .Build();

        var batchEventProcessor = new BatchEventProcessor.Builder()
            .WithMaxBatchSize(10)
            .WithFlushInterval(TimeSpan.FromSeconds(30))
            .Build();

        var optimizely = new Optimizely(
            configManager: projectConfigManager,
            // ... Other parameters
            eventProcessor: batchEventProcessor
        );
    }
}
```

:::hint{type="danger"}
**Warning**
The maximum payload size is 3.5 MB. Optimizely rejects requests with a 400 response code, `Bad Request Error`, if the batch payload exceeds this limit.

The size limitation is because of the [Optimizely Events API](docId:822ZQOB6Zk2-kWJKavBvA), which Feature Experimentation uses to send data to Optimizely.

The most common cause of a large payload size is a high batch size. If your payloads exceed the size limit, try configuring a smaller batch size.
:::

## `BatchEventProcessor`

`BatchEventProcessor` is an implementation of `EventProcessor` where events are batched. The class maintains a single consumer thread that pulls events off of the `BlockingCollection` and buffers them for either a configured batch size or a maximum duration before the resulting `LogEvent` is sent to the `EventDispatcher` and `NotificationCenter`.

The following properties can be used to customize the BatchEventProcessor configuration *using the Builder class*

| Property               | Default value          | Description                                                                                                                                  |
| ---------------------- | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **EventDispatcher**    | DefaultEventDispatcher | Used to dispatch event payload to Optimizely.                                                                                                |
| **BatchSize**          | 10                     | The maximum number of events to batch before dispatching. Once this number is reached, all queued events are flushed and sent to Optimizely. |
| **FlushInterval**      | 30000 (30 Seconds)     | Milliseconds to wait before batching and dispatching events.                                                                                 |
| **EventQueue**         | 1000                   | BlockingCollection that queues individual events to be batched and dispatched by the executor.                                               |
| **NotificationCenter** | null                   | Notification center instance to be used to trigger any notifications.                                                                        |

For information, see [Initialize the C# SDK](docId\:xoqfGXRS2U3DUf8EUgtsC).

## Side effects

The table lists other Optimizely functionality that may be triggered by using this class.

| Functionality                        | Description                                                                                                                                                                                                                                                                                   |
| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \`LogEvent\` (see following section) | Whenever the event processor produces a batch of events, a \`LogEvent\` object is created using the \`EventFactory\`. It contains batch of conversion and decision events. This object is dispatched using the provided event dispatcher and also it is sent to the notification subscribers. |
| Notification Listeners               | Flush invokes the \`LOGEVENT\` \[notification listener]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/set-up-notification-listener-csharp) if this listener is subscribed to.                                                                                         |

### Register LogEvent listener

To register a `LogEvent` listener:

```csharp
NotificationCenter.AddNotification(
    NotificationCenter.NotificationType.LogEvent, 
    new NotificationCenter.LogEventCallback((logevent) => {
        // Your code here
    })
);
```

### `LogEvent`

`LogEvent` object gets created using [`EventFactory`](https://github.com/optimizely/csharp-sdk/blob/master/OptimizelySDK/Event/EventFactory.cs). It represents the batch of decision and conversion events Optimizely sends to the Optimizely backend.

| Object                 | Type                | Description                                                                                                                                                                                                                                                                                                              |
| ---------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **Url** Required       | string              | URL to dispatch log event to.                                                                                                                                                                                                                                                                                            |
| **Params**\ Required   | Dictionary\\        | Parameters to be set in the log event. It contains \[\`EventBatch\`]\(https\://github.com/optimizely/csharp-sdk/blob/master/OptimizelySDK/Event/Entity/EventBatch.cs) of all \`UserEvents\` inside \[\`Visitors\`]\(https\://github.com/optimizely/csharp-sdk/blob/master/OptimizelySDK/Event/Entity/EventBatch.cs#L45). |
| **HttpVerb**\ Required | string              | The HTTP verb to use when dispatching the log event. It can be \`GET\` or \`POST\`.                                                                                                                                                                                                                                      |
| **Headers**            | Dictionary\ Headers | Headers to be set when sending the request.                                                                                                                                                                                                                                                                              |

## Dispose Optimizely on application exit

If you enable event batching, **you must call** the Close method (`optimizely.Dispose()`) before exiting. This ensures that queued events are flushed when possible to avoid data loss.

:::hint{type="info"}
**Warning**
Because the Optimizely client maintains a buffer of queued events, you must call `Dispose()` on the  instance before shutting down your application or whenever dereferencing the instance.
:::

| Method        | Description                                                                                                                       |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| **Dispose()** | Stops all timers and flushes the event queue. This method will also stop any timers that are happening for the data-file manager. |
