# .NET SDK

> One page of the LogDuck documentation. Every page is listed in https://logduck.com/llms.txt, and https://logduck.com/llms-full.txt is all of them in one file.

The official .NET client library for LogDuck. Supports .NET 8, 9, and 10. Provides dependency injection via `AddLogDuck()`, automatic retries with idempotency keys, and configurable error handling.

## Installation

```bash
dotnet add package LogDuck
```

## Minimal example

The fewest settings that work — an API key and a source.

```csharp
// Program.cs
using LogDuck;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddLogDuck(options =>
{
    options.ApiKey = builder.Configuration["LogDuck:ApiKey"]!;
    options.Source = "checkout-api";
});
```

```csharp
// CheckoutService.cs
using LogDuck;

public class CheckoutService(ILogDuckClient logDuck)  // injected by AddLogDuck above
{
    public async Task OnOrderPlaced(string orderId)
    {
        await logDuck.SendEventAsync(new LogDuckEvent { Type = "order.placed" });
    }
}
```

## Complete example

Every option, with its default. Only the first two are required; the rest are shown set to the value you get by omitting them.

```csharp
// Program.cs
using LogDuck;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddLogDuck(options =>
{
    options.ApiKey = builder.Configuration["LogDuck:ApiKey"]!;  // required - starts with ld_
    options.Source = "checkout-api";                            // required - max 200 chars
    options.ThrowOnError = false;                               // optional, default false - true throws instead of returning null
    options.RetryEnabled = true;                                // optional, default true  - retry once on 5xx, network errors and 429
    options.MaxRetryDelay = TimeSpan.FromSeconds(10);           // optional, default 10s   - never block your caller longer than this
    options.Timeout = TimeSpan.FromSeconds(30);                 // optional, default 30s   - per-request timeout
});

// CheckoutService.cs
using LogDuck;

public class CheckoutService(ILogDuckClient logDuck)  // injected by AddLogDuck above
{
    public async Task OnOrderPlaced(string orderId)   // e.g. "order_9127"
    {
        var sessionId = "sess_8f21c";                 // your own session identifier

        LogDuckResponse? response = await logDuck.SendEventAsync(new LogDuckEvent
        {
            Type = "order.placed",                    // required - max 100 chars, naming rule below
            Subject = orderId,                        // optional - what the event is about, max 500 chars
            SessionId = sessionId,                    // optional - groups related events, max 256 chars
            Message = "Order placed on the Pro plan", // optional - the push notification body, max 500
            Time = DateTimeOffset.UtcNow,             // optional - defaults to when the server received it
            Data = new Dictionary<string, object?>    // optional - any JSON-serialisable values
            {
                ["total"] = 4999,
                ["currency"] = "NOK"
            },
            Emoji = "🛒"                                // optional - shown next to the event, max 10 chars
        });

        // null when the send failed and ThrowOnError is false (the default).
        Console.WriteLine(response?.EventId);
    }
}
```

## Retries and idempotency

Each request includes a unique `Idempotency-Key` header (a UUID). The same key is reused across retries, so the server deduplicates and a retry can never create a duplicate event.

A rate-limited request (429) is retried once, waiting for the period the server asks for — but never longer than `MaxRetryDelay`. The limit resets in one-minute windows, so blocking a caller for the full window inside a logging call is worse than failing. Past the cap the client gives up immediately and the failure carries `LogDuckException.RetryAfter`, so you can queue the event rather than lose it.

Full reference and changelog: [github.com/Log-Duck/logduck-dotnet](https://github.com/Log-Duck/logduck-dotnet)
