private void Initialize(HttpOutputConfiguration configuration, Implementation.IHttpClient httpClient) { string errorMessage; Debug.Assert(configuration != null); Debug.Assert(this.healthReporter != null); this.httpClient = httpClient ?? new StandardHttpClient(); this.configuration = configuration; if (string.IsNullOrWhiteSpace(this.configuration.ServiceUri)) { var errMsg = $"{nameof(HttpOutput)}: no ServiceUri configured"; healthReporter.ReportProblem(errMsg); throw new Exception(errMsg); } string userName = configuration.BasicAuthenticationUserName; string password = configuration.BasicAuthenticationUserPassword; bool credentialsIncomplete = string.IsNullOrWhiteSpace(userName) ^ string.IsNullOrWhiteSpace(password); if (credentialsIncomplete) { errorMessage = $"{nameof(configuration)}: for basic authentication to work both user name and password must be specified"; healthReporter.ReportWarning(errorMessage, EventFlowContextIdentifiers.Configuration); userName = password = null; } if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password)) { string httpAuthValue = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password))); this.httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", httpAuthValue); } switch (this.configuration.Format) { case HttpOutputFormat.Json: case HttpOutputFormat.JsonLines: if (string.IsNullOrWhiteSpace(this.configuration.HttpContentType)) { this.configuration.HttpContentType = "application/json"; } break; } foreach (KeyValuePair <string, string> header in configuration.Headers) { this.httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } SerializerSettings = EventFlowJsonUtilities.GetDefaultSerializerSettings(); }
public HttpOutput(HttpOutputConfiguration configuration, IHealthReporter healthReporter, Implementation.IHttpClient httpClient = null) { Requires.NotNull(configuration, nameof(configuration)); Requires.NotNull(healthReporter, nameof(healthReporter)); this.healthReporter = healthReporter; // Clone the configuration instance since we are going to hold onto it (via this.connectionData) Initialize(configuration.DeepClone(), httpClient); }
public HttpOutput(IConfiguration configuration, IHealthReporter healthReporter, Implementation.IHttpClient httpClient = null) { Requires.NotNull(configuration, nameof(configuration)); Requires.NotNull(healthReporter, nameof(healthReporter)); this.healthReporter = healthReporter; var httpOutputConfiguration = new HttpOutputConfiguration(); try { configuration.Bind(httpOutputConfiguration); } catch { healthReporter.ReportProblem($"Invalid {nameof(HttpOutput)} configuration encountered: '{configuration.ToString()}'", EventFlowContextIdentifiers.Configuration); throw; } Initialize(httpOutputConfiguration, httpClient); }