Structured Logging in .NET with Serilog and ClickHouse
Serilog's ClickHouse sink lets an ASP.NET service write structured logs directly into a ClickHouse table with one WriteTo.ClickHouse() call. A fluent C# schema builder controls the table: typed JSON property hints, promoted columns such as CorrelationId, a text inverted index on messages, a MergeTree ORDER BY key and a TTL. The demo also covers correlation ID middleware via LogContext, single per-request summary events with UseSerilogRequestLogging and IDiagnosticContext, and flushing batched events with Log.CloseAndFlushAsync on shutdown. For production, manage the schema externally rather than letting the sink create it, and tune compression codecs and Kubernetes-aware sort keys.
Related posts
- ClickHouse and OpenTelemetry
ClickHouse mediumAdept
ClickHouse now officially supports the OpenTelemetry Collector exporter for ClickHouse, which has reached beta for logs and traces. The exporter ships a "one size fits most" default schema. For production, ClickHouse recommends disabling automatic table creation, because concurrent exporters race to create tables, and defining your own tables or materialized views keyed to your query patterns. The LogHouse schema behind ClickHouse's 43 PB internal logging platform serves as the worked example. It orders by (PodName, Timestamp), uses LowCardinality for string columns under about 10k distinct values, applies ZSTD(1) because data lives on S3, and puts Bloom-filter indexes on map keys and values, which are not yet tuned.
- Building a .NET API Gateway with ClickHouse and Aspire
ClickHouse mediumAdept
A sample .NET API gateway uses Aspire to orchestrate a ClickHouse container, a YARP gateway, backend APIs, a load generator, an analytics API and a Blazor dashboard. Aspire provides live OpenTelemetry metrics and ClickHouse stores the full request history for aggregate analytics. The gateway middleware never blocks on the database: it enqueues rows into a bounded System.Threading.Channels queue with DropWrite, counts drops as a metric, and a background service batch-inserts via RowBinary with async_insert and wait_for_async_insert. The ClickHouse schema uses LowCardinality columns, daily partitions, a sorting key, a 30-day TTL, and an AggregatingMergeTree materialized view with -State/-Merge combinators so percentile dashboards stay fast regardless of log volume.
- Sending Windows Event Logs to ClickHouse with Fluent Bit
ClickHouse mediumAdept
Fluent Bit's winevtlog input can collect Windows Event Logs, including the Sysmon/Operational and Security channels, and ship them to ClickHouse over HTTP as JSONEachRow with async_insert enabled. Sysmon uses SwiftOnSecurity's MITRE ATT&CK-aligned configuration, and logs land in a MergeTree table built on the experimental JSON object type, whose inferred schema is shown. Grafana's ClickHouse plugin provides threat-hunting dashboards, and SysmonSimulator generates test events. For production, map frequent fields such as Computer to explicit LowCardinality columns and put them in the ORDER BY key instead of relying on dynamic JSON with tuple().
- How trip.com migrated from Elasticsearch and built a 50PB logging solution with ClickHouse
ClickHouse mediumAdept
Trip.com replaced its Elasticsearch-based centralized logging platform with ClickHouse after hitting stability, ingestion and cost limits at 4PB, and has since grown it toward 50PB. The migration used a ReplicatedMergeTree schema with daily partitions, ZSTD codecs, LowCardinality columns, tag key/value arrays and tokenbf_v1 bloom-filter indexes. Kibana was extended with custom ClickHouse panels, and scripts migrated 95% of dashboards automatically. The result was over 50% storage savings and queries 4–30x faster (P90 under 300ms). Further growth caused ZooKeeper-driven DDL timeouts, which led trip.com to split into multiple clusters and to evaluate ClickHouse Cloud with SharedMergeTree.
- Clever ingests 200x more logs at the same cost with ClickHouse Cloud
ClickHouse mediumAdept
Clever replaced a split Datadog/Athena logging setup with ClickHouse Cloud. It now indexes all ~150 TB/month of logs from 400 services and 200 Lambdas with 60-day retention, a 200x increase in searchable volume, at no extra cost and with 10x compression. The schema extracts hot fields into columns and stores the rest as raw JSON plus an attributes map. It uses token bloom filter indexes at granularity 32 for full-text search, date partitions with a 60-day TTL, and orders by negative Unix timestamp for an 8-10x query speedup. Resilience comes from separate dev and per-region prod services joined via remoteSecure and merge tables, all managed with Terraform and Goose. A custom LogQL-to-SQL layer keeps Grafana familiar for engineers.