Overview
Send logs to Datadog from your iOS and Android applications built with .NET MAUI using Datadog’s Datadog.Maui client-side logging library and use the following features:
- Log to Datadog in JSON format natively.
- Forward managed C# exceptions.
- Record real client IP addresses and User-Agents.
- Optimize network usage with automatic bulk posts.
- Enrich logs with global attributes, user info, and account info.
Setup
Before sending logs, initialize the Datadog SDK by following the .NET MAUI setup documentation. Make sure DdSdk.Initialize (or the .UseDatadog(...) builder extension) runs before DdLogs.Enable().
Enable the Logs feature.
With the builder extension:
using Datadog.Maui;
using Datadog.Maui.Configuration;
using Datadog.Maui.Hosting;
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseDatadog(new DdSdkConfiguration
{
ClientToken = "<CLIENT_TOKEN>",
Environment = "<ENV_NAME>",
TrackingConsent = TrackingConsent.Granted,{% region-param key="maui_site_config" /%}
})
.UseDatadogLogs();
return builder.Build();
}
Or with the standalone API, call DdLogs.Enable() after DdSdk.Initialize:
using Datadog.Maui;
DdLogs.Enable();
Send a log entry with one of the following methods:
DdLogs.Debug("A debug message.");
DdLogs.Info("Some relevant information?");
DdLogs.Warn("An important warning...");
DdLogs.Error("An error was met!");
Log messages are sent to the Datadog Log Explorer and appear under the service name you configured at SDK initialization.
Enrich your logs
Logs inherit context already set on the core SDK. You don’t need to attach the same metadata to every call - set it once and every emitted log carries it.
Global attributes
Attributes added with DdSdk.AddAttribute (or DdSdk.AddAttributes) are attached to every log, RUM event, and trace emitted after the call. Use them for cross-cutting context such as feature flags, experiment IDs, or release channels.
DdSdk.AddAttribute("plan", "premium");
DdSdk.AddAttribute("experiment", "new-checkout-flow");
// Remove when the context no longer applies
DdSdk.RemoveAttribute("experiment");
Use DdSdk.SetUserInfo to attach an identity to every log line for the current user. This makes it easier to triage logs that relate to a specific user when investigating a support ticket.
DdSdk.SetUserInfo("user-123", "Jane Doe", "[email protected]",
new Dictionary<string, object> { { "plan", "premium" } });
See Track user sessions for the full attribute reference.
For B2B applications, DdSdk.SetAccountInfo attaches an account identity to every log. Use it together with — not instead of — user info.
DdSdk.SetAccountInfo("acct-456", "Acme Corp",
new Dictionary<string, object> { { "tier", "enterprise" } });
Custom endpoint
For testing against a local mock server, an on-premises Datadog deployment, or a corporate proxy, pass a CustomEndpoint when enabling Logs:
DdLogs.Enable(new DdLogsConfiguration
{
CustomEndpoint = "https://logs-proxy.example.com/v1/input"
});
If you also need to route the rest of the SDK traffic through a proxy, configure ProxyConfiguration on the SDK itself. See Advanced Configuration > Proxy configuration.
Batch collection
All logs are first stored on the local device in batches. Each batch follows the intake specification. Batches are sent as soon as the network is available and the battery is high enough that the Datadog SDK does not impact the end user’s experience. If the network is not available while your application is in the foreground, or if an upload of data fails, the batch is kept until it can be sent successfully.
This means that even if users open your application while offline, no data is lost.
The data on disk is automatically discarded if it gets too old. This makes sure the SDK does not use too much disk space.
Before data is uploaded to Datadog, it is stored in cleartext in your application’s cache directory.
Further Reading
Additional helpful documentation, links, and articles: