protected override void Write(LogEventInfo logEvent) { GraylogMessageBuilder messageBuilder = new GraylogMessageBuilder() .WithCustomProperty("facility", this.Facility) .WithProperty("short_message", logEvent.Message) .WithProperty("host", this.Host) .WithLevel(logEvent.Level) .WithCustomProperty("logger_name", logEvent.LoggerName); if (this.Parameters != null && this.Parameters.Any()) { Dictionary<string, string> paramsDictionary = this.Parameters .Select(p => new KeyValuePair<string, string>(p.Name, p.Layout.Render(logEvent))) .ToDictionary(pair => pair.Key, pair => pair.Value); messageBuilder.WithCustomPropertyRange(paramsDictionary); } if (logEvent.Exception != null) { if (!string.IsNullOrEmpty(logEvent.Exception.Message)) messageBuilder.WithCustomProperty("exception_message", logEvent.Exception.Message); if (!string.IsNullOrEmpty(logEvent.Exception.StackTrace)) messageBuilder.WithCustomProperty("exception_stack_trace", logEvent.Exception.StackTrace); } using (var httpClient = new HttpClient()) { httpClient.BaseAddress = new Uri(string.Format("{0}:{1}/gelf", this.GraylogServer, this.GraylogPort)); httpClient.DefaultRequestHeaders.ExpectContinue = false; // Expect (100) Continue breaks the graylog server HttpResponseMessage httpResponseMessage = httpClient.PostAsync("", new StringContent(messageBuilder.Render(), Encoding.UTF8, "application/json")).Result; } }
protected override void Write(LogEventInfo logEvent) { GraylogMessageBuilder messageBuilder = new GraylogMessageBuilder() .WithCustomProperty("_facility", Facility) .WithProperty("short_message", logEvent.FormattedMessage) .WithProperty("host", Host) .WithLevel(logEvent.Level) .WithCustomProperty("_logger_name", logEvent.LoggerName); var properties = GetAllProperties(logEvent); foreach (var property in properties) { try { if (!string.IsNullOrEmpty(property.Key)) { if (Convert.GetTypeCode(property.Value) != TypeCode.Object) { messageBuilder.WithCustomProperty(property.Key, property.Value); } else { messageBuilder.WithCustomProperty(property.Key, property.Value?.ToString()); } } } catch (Exception ex) { InternalLogger.Error(ex, "GraylogHttp(Name={0}): Fail to handle LogEvent Properties", Name); } } if (logEvent.Exception != null) { if (!string.IsNullOrEmpty(logEvent.Exception.Message)) { messageBuilder.WithCustomProperty("_exception_message", logEvent.Exception.Message); } if (!string.IsNullOrEmpty(logEvent.Exception.StackTrace)) { messageBuilder.WithCustomProperty("_exception_stack_trace", logEvent.Exception.StackTrace); } } try { // Ensure to reuse the same HttpClient. See also: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ _httpClient.PostAsync(_requestAddress, new StringContent(messageBuilder.Render(logEvent.TimeStamp), Encoding.UTF8, "application/json")).Result.EnsureSuccessStatusCode(); } catch (AggregateException ex) { foreach (var inner in ex.Flatten().InnerExceptions) { InternalLogger.Error(inner, "GraylogHttp(Name={0}): Fail to post LogEvents", Name); } } catch (Exception ex) { InternalLogger.Error(ex, "GraylogHttp(Name={0}): Fail to post LogEvents", Name); } }
protected override void Write(LogEventInfo logEvent) { if (logEvent == null) { return; } GraylogMessageBuilder messageBuilder = new GraylogMessageBuilder() .WithProperty("short_message", logEvent.FormattedMessage) .WithProperty("host", Host) .WithLevel(logEvent.Level) .WithSyslogLevelName(logEvent.Level) .WithCustomProperty("logger_name", logEvent.LoggerName); if (AddNLogLevelName) { messageBuilder.WithNLogLevelName(logEvent.Level); } if (!string.IsNullOrEmpty(Facility)) { messageBuilder.WithCustomProperty("facility", Facility); } var properties = GetAllProperties(logEvent); foreach (var property in properties) { try { if (!string.IsNullOrEmpty(property.Key)) { if (Convert.GetTypeCode(property.Value) != TypeCode.Object) { messageBuilder.WithCustomProperty(property.Key, property.Value); } else { messageBuilder.WithCustomProperty(property.Key, property.Value?.ToString()); } } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { InternalLogger.Error(ex, "GraylogHttp(Name={0}): Fail to handle LogEvent Properties", Name); } } if (logEvent.Exception != null) { if (!string.IsNullOrEmpty(logEvent.Exception.Message)) { messageBuilder.WithCustomProperty("_exception_message", logEvent.Exception.Message); } if (!string.IsNullOrEmpty(logEvent.Exception.StackTrace)) { messageBuilder.WithCustomProperty("_exception_stack_trace", logEvent.Exception.StackTrace); } } try { _policy.Execute(() => { var content = new StringContent(messageBuilder.Render(logEvent.TimeStamp), Encoding.UTF8, "application/json"); return(_httpClient.PostAsync(_requestAddress, content).Result.EnsureSuccessStatusCode()); }); } catch (BrokenCircuitException) { InternalLogger.Error( "GraylogHttp(Name={0}): The Graylog server seems to be inaccessible, the log messages were not sent to the server.", Name); } }