TechnitiumSoftware/DnsServer · error · FormatException

Failed to add header '{pair.Key}'.

Error message

Failed to add header '{pair.Key}'.

What it means

Thrown by CustomHttpClient.Configure when HttpClient.DefaultRequestHeaders.TryAddWithoutValidation returns false for a header read from the LogExporter HTTP target configuration. .NET rejects headers with invalid names or values, and the app surfaces that as FormatException with the offending key. It stops the export strategy from silently dropping headers.

Source

Thrown at Apps/LogExporterApp/Strategy/HttpExportStrategy.cs:100

        }

        #endregion

        public class CustomHttpClient : IHttpClient
        {
            readonly HttpClient _httpClient;

            public CustomHttpClient()
            {
                _httpClient = new HttpClient();
            }

            public void Configure(IConfiguration configuration)
            {
                foreach (IConfigurationSection pair in configuration.GetChildren())
                {
                    if (!_httpClient.DefaultRequestHeaders.TryAddWithoutValidation(pair.Key, pair.Value))
                        throw new FormatException($"Failed to add header '{pair.Key}'.");
                }
            }

            public void Dispose()
            {
                _httpClient?.Dispose();
                GC.SuppressFinalize(this);
            }

            public async Task<HttpResponseMessage> PostAsync(string requestUri, Stream contentStream, CancellationToken cancellationToken)
            {
                StreamContent content = new StreamContent(contentStream);
                content.Headers.Add("Content-Type", "application/json");

                return await _httpClient
                    .PostAsync(requestUri, content, cancellationToken)
                    .ConfigureAwait(false);
            }

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Inspect the LogExporter HTTP target 'headers' section and find the key named in the message.
  2. Correct the header name to a valid RFC 7230 token (no spaces, control chars, or delimiters).
  3. Strip any CR/LF or stray quotes from the header value.
  4. Reload the LogExporterApp config.

Example fix

// before
"headers": { "Content Type": "application/json" }
// after
"headers": { "Content-Type": "application/json" }
Defensive patterns

Strategy: validation

Validate before calling

foreach (IConfigurationSection pair in configuration.GetChildren())
{
    if (string.IsNullOrWhiteSpace(pair.Key) || pair.Key.Any(c => c <= 32 || c == ':' || c > 126))
        throw new ConfigValidationException($"Invalid HTTP header name '{pair.Key}'.");
    if (pair.Value != null && (pair.Value.IndexOfAny(new[] {'\r','\n'}) >= 0))
        throw new ConfigValidationException($"HTTP header '{pair.Key}' value contains CR/LF.");
}

Prevention

When it happens

Trigger: The LogExporter HTTP target config contains a 'headers' section where a key or value fails .NET header validation — e.g. a header name with spaces or control characters, or a value containing CR/LF. Each child section becomes one header via TryAddWithoutValidation.

Common situations: Typo in a header name, copy-pasting a header with a colon in the key, a value containing a newline, or a misconfigured templated header.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/9869af1b9b202258. Report an issue: GitHub.