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
- Inspect the LogExporter HTTP target 'headers' section and find the key named in the message.
- Correct the header name to a valid RFC 7230 token (no spaces, control chars, or delimiters).
- Strip any CR/LF or stray quotes from the header value.
- 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
- Validate header names against RFC 7230 token rules before adding.
- Reject header values containing CR/LF.
- Lint the LogExporter headers block on deploy.
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
- No 'healthCheck' was configured for: {appRecordName}
- No 'healthCheck' was configured for: {appRecordName}
- Invalid application configuration.
- Syslog protocol is not supported: {protocol}
- Local end point group map contains an invalid end point: {lo
AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13).
Data as JSON: /api/errors/9869af1b9b202258.
Report an issue: GitHub.