/// <summary> /// Get a GelfMessage with info about logging event. /// </summary> /// <param name="loggingEvent">Logging event</param> /// <returns>GelfMessage</returns> protected virtual GelfMessage GetGelfMessage(LoggingEvent loggingEvent) { var fullMessage = GetFullMessage(loggingEvent); var gelfMessage = new GelfMessage { Facility = Facility ?? "GELF", File = string.Empty, FullMesage = fullMessage, Host = this.GetLoggingHostName(), Level = this.GetSyslogSeverity(loggingEvent.Level), Line = string.Empty, ShortMessage = this.GetShortMessage(fullMessage, loggingEvent), TimeStamp = loggingEvent.TimeStamp, Version = GELF_VERSION, }; if (this.IncludeLocationInformation) { gelfMessage.File = loggingEvent.LocationInformation.FileName; gelfMessage.Line = loggingEvent.LocationInformation.LineNumber; } return(gelfMessage); }
internal string BuildFromLoggingEvent(GelfMessage gelfMessage, Dictionary<string, string> innerAdditionalFields) { var gelfJsonMessage = JsonConvert.SerializeObject(gelfMessage); var jsonObject = JObject.Parse(gelfJsonMessage); this.AddAdditionalFields(jsonObject, innerAdditionalFields); return jsonObject.ToString(); }
internal string BuildFromLoggingEvent(GelfMessage gelfMessage, Dictionary <string, string> innerAdditionalFields) { var gelfJsonMessage = JsonConvert.SerializeObject(gelfMessage); var jsonObject = JObject.Parse(gelfJsonMessage); this.AddAdditionalFields(jsonObject, innerAdditionalFields); return(jsonObject.ToString()); }
/// <summary>Append a log event on graylog. </summary> /// <param name="loggingEvent">The logging event. </param> protected override void Append(LoggingEvent loggingEvent) { GelfMessage gelfMessage = this.GetGelfMessage(loggingEvent); Dictionary <string, string> additionalFields = this.GetAdditionalFields(loggingEvent); string gelfJsonString = new GelfJsonBuilder().BuildFromLoggingEvent( gelfMessage, additionalFields); if (UseUdpTransport) { SendGelfMessageToGrayLog(gelfJsonString); } else { SendAmqpMessageToGrayLog(gelfJsonString); } }
internal String BuildFromLoggingEvent( String message, log4net.Core.LoggingEvent loggingEvent, String hostName, String facility, Boolean isConfiguredToIncludeLocationInformation, Dictionary<String, String> innerAdditionalFields) { var fullMessage = GetFullMessage( message, loggingEvent ); var gelfMessage = new GelfMessage { Facility = (facility ?? "GELF"), File = isConfiguredToIncludeLocationInformation ? loggingEvent.LocationInformation.FileName : String.Empty, FullMesage = fullMessage, Host = hostName, Level = GetSyslogSeverity( loggingEvent.Level ), Line = isConfiguredToIncludeLocationInformation ? loggingEvent.LocationInformation.LineNumber : String.Empty, ShortMessage = GetShortMessage( fullMessage ), TimeStamp = GetUnixTimestamp( loggingEvent.TimeStamp ), Version = GELF_VERSION, }; return GetGelfJsonMessage( loggingEvent, innerAdditionalFields, gelfMessage ); }
internal string BuildFromLoggingEvent(log4net.Core.LoggingEvent loggingEvent, string hostName, string facility, bool isConfiguredToIncludeLocationInformation, Dictionary <string, string> innerAdditionalFields) { var fullMessage = GetFullMessage(loggingEvent); var gelfMessage = new GelfMessage { Facility = (facility ?? "GELF"), File = "", FullMesage = fullMessage, Host = hostName, Level = GetSyslogSeverity(loggingEvent.Level), Line = "", ShortMessage = GetShortMessage(fullMessage), TimeStamp = loggingEvent.TimeStamp, Version = GELF_VERSION, }; if (isConfiguredToIncludeLocationInformation) { gelfMessage.File = loggingEvent.LocationInformation.FileName; gelfMessage.Line = loggingEvent.LocationInformation.LineNumber; } return(GetGelfJsonMessage(loggingEvent, innerAdditionalFields, gelfMessage)); }
internal string BuildFromLoggingEvent(string message, LoggingEvent loggingEvent, string hostName, string facility, bool isConfiguredToIncludeLocationInformation, Dictionary<string, string> innerAdditionalFields) { var fullMessage = GetFullMessage(message, loggingEvent); var gelfMessage = new GelfMessage { Facility = (facility ?? "GELF"), File = "", FullMesage = fullMessage, Host = hostName, Level = SeverityLevelConverter.LogLevelSeverityToSysLogLevelSeverity(loggingEvent.Level), Line = "", ShortMessage = GetShortMessage(fullMessage), TimeStamp = loggingEvent.TimeStamp, Version = GELF_VERSION, }; if (isConfiguredToIncludeLocationInformation && loggingEvent.LocationInformation != null) { gelfMessage.File = loggingEvent.LocationInformation.FileName; gelfMessage.Line = loggingEvent.LocationInformation.LineNumber; } return GetGelfJsonMessage(loggingEvent, innerAdditionalFields, gelfMessage); }
private String GetGelfJsonMessage( log4net.Core.LoggingEvent loggingEvent, Dictionary<String, String> innerAdditionalFields, GelfMessage gelfMessage) { var gelfJsonMessage = JsonConvert.SerializeObject( gelfMessage ); var jsonObject = JObject.Parse( gelfJsonMessage ); AddInnerAdditionalFields( jsonObject, innerAdditionalFields ); AddLoggingEventAdditionalFields( jsonObject, loggingEvent ); return jsonObject.ToString(); }
private string GetGelfJsonMessage(log4net.Core.LoggingEvent loggingEvent, Dictionary <string, string> innerAdditionalFields, GelfMessage gelfMessage) { var gelfJsonMessage = JsonConvert.SerializeObject(gelfMessage); var jsonObject = JObject.Parse(gelfJsonMessage); AddInnerAdditionalFields(jsonObject, innerAdditionalFields); AddLoggingEventAdditionalFields(jsonObject, loggingEvent); return(jsonObject.ToString()); }
/// <summary> /// Get a GelfMessage with info about logging event. /// </summary> /// <param name="loggingEvent">Logging event</param> /// <returns>GelfMessage</returns> protected virtual GelfMessage GetGelfMessage(LoggingEvent loggingEvent) { var fullMessage = GetFullMessage(loggingEvent); var gelfMessage = new GelfMessage { Facility = Facility ?? "GELF", File = string.Empty, FullMesage = fullMessage, Host = this.GetLoggingHostName(), Level = this.GetSyslogSeverity(loggingEvent.Level), Line = 0, ShortMessage = this.GetShortMessage(fullMessage, loggingEvent), TimeStamp = loggingEvent.TimeStamp, Version = GELF_VERSION, }; if (this.IncludeLocationInformation) { gelfMessage.File = loggingEvent.LocationInformation.FileName; int line = 0; int.TryParse(loggingEvent.LocationInformation.LineNumber, out line); gelfMessage.Line = line; } return gelfMessage; }
/// <summary> /// Creates the JSON String for a given <code>LoggingEvent</code>. /// The "short_message" of the GELF message is max 50 chars long. /// Message building and skipping of additional fields etc is based on /// https://github.com/Graylog2/graylog2-docs/wiki/GELF from Jan 7th 2011. /// </summary> /// <param name="loggingEvent"> The logging event to base the JSON creation on</param> /// <returns>GelfMessage as JSON</returns> private string CreateGelfJsonFromLoggingEvent(log4net.Core.LoggingEvent loggingEvent) { var fullMessage = loggingEvent.RenderedMessage; if (loggingEvent.ExceptionObject != null) { fullMessage = String.Format("{0} - {1}. {2}. {3}.", fullMessage, loggingEvent.ExceptionObject.Source, loggingEvent.ExceptionObject.Message, loggingEvent.ExceptionObject.StackTrace); } var shortMessage = fullMessage; if (shortMessage.Length > SHORT_MESSAGE_LENGTH) { shortMessage = shortMessage.Substring(0, SHORT_MESSAGE_LENGTH - 1); } var gelfMessage = new GelfMessage { Facility = (this.Facility ?? "GELF"), File = "", FullMesage = fullMessage, Host = LoggingHostName, Level = GetSyslogSeverity(loggingEvent.Level), Line = "", ShortMessage = shortMessage, TimeStamp = loggingEvent.TimeStamp, Version = GELF_VERSION }; //only set location information if configured if (IncludeLocationInformation) { gelfMessage.File = loggingEvent.LocationInformation.FileName; gelfMessage.Line = loggingEvent.LocationInformation.LineNumber; } var gelfJsonMessage = JsonConvert.SerializeObject(gelfMessage); var jsonObject = JObject.Parse(gelfJsonMessage); //add additional fields and prepend with _ if not present already if (innerAdditionalFields != null) { foreach (var item in innerAdditionalFields) { AddAdditionalFields(item.Key, item.Value, jsonObject); } } //add additional fields and prepend with _ if not present already if (loggingEvent.Properties != null) { foreach (DictionaryEntry item in loggingEvent.Properties) { var key = item.Key as string; if (key != null) { AddAdditionalFields(key, item.Value as string, jsonObject); } } } return jsonObject.ToString(); }