/// <summary> /// Builds a syslog-compatible message using the information we have available. /// </summary> /// <param name="facility">Syslog Facility to transmit message from</param> /// <param name="priority">Syslog severity level</param> /// <param name="time">Time stamp for log message</param> /// <param name="sender">Name of the subsystem sending the message</param> /// <param name="body">Message text</param> /// <returns>Byte array containing formatted syslog message</returns> private byte[] BuildSyslogMessage(SyslogFacility facility, SyslogSeverity priority, DateTime time, string sender, string body) { // Get sender machine name string machine = MachineName + " "; // Calculate PRI field int calculatedPriority = (int)facility * 8 + (int)priority; string pri = "<" + calculatedPriority.ToString(CultureInfo.InvariantCulture) + ">"; string timeToString = time.ToString("MMM dd HH:mm:ss "); sender = sender + ": "; string[] strParams = { pri, timeToString, machine, sender, body, Environment.NewLine }; return(Encoding.ASCII.GetBytes(string.Concat(strParams))); }
/// <summary> /// Builds a syslog-compatible message using the information we have available. /// </summary> /// <param name="facility">Syslog Facility to transmit message from</param> /// <param name="priority">Syslog severity level</param> /// <param name="time">Time stamp for log message</param> /// <param name="sender">Name of the subsystem sending the message</param> /// <param name="body">Message text</param> /// <returns>Byte array containing formatted syslog message</returns> private static byte[] buildSyslogMessage(SyslogFacility facility, SyslogSeverity priority, DateTime time, string sender, string body) { // Get sender machine name string machine = System.Net.Dns.GetHostName() + " "; // Calculate PRI field int calculatedPriority = (int)facility * 8 + (int)priority; string pri = "<" + calculatedPriority.ToString() + ">"; string timeToString = time.ToString("MMM dd HH:mm:ss "); sender = sender + ": "; string[] strParams = { pri, timeToString, machine, sender, body }; return(Encoding.ASCII.GetBytes(string.Concat(strParams))); }
/// <summary> /// Builds a syslog-compatible message using the information we have available. /// </summary> /// <param name="facility">Syslog Facility to transmit message from</param> /// <param name="priority">Syslog severity level</param> /// <param name="time">Time stamp for log message</param> /// <param name="sender">Name of the subsystem sending the message</param> /// <param name="body">Message text</param> /// <returns>Byte array containing formatted syslog message</returns> private byte[] BuildSyslogMessage(SyslogFacility facility, SyslogSeverity priority, DateTime time, string sender, string body) { // Get sender machine name string machine = Dns.GetHostName() + " "; // Calculate PRI field int calculatedPriority = (int)facility * 8 + (int)priority; string pri = "<" + calculatedPriority.ToString(CultureInfo.InvariantCulture) + ">"; string timeToString = time.ToString("MMM dd HH:mm:ss "); sender = sender + ": "; if (!String.IsNullOrEmpty(CustomPrefix)) { body = String.Format("[{0}] {1}", CustomPrefix, body); } string[] strParams = { pri, timeToString, machine, sender, body, Environment.NewLine }; return(Encoding.ASCII.GetBytes(string.Concat(strParams))); }
private static int CalculatePriorityValue(SyslogFacility facility, SyslogSeverity severity) { return((int)facility * 8 + (int)severity); }
/// <summary>Builds rfc-5424 compatible message</summary> /// <param name="logEvent">The NLog.LogEventInfo</param> /// <param name="facility">Syslog Facility to transmit message from</param> /// <param name="severity">Syslog severity level</param> /// <param name="body">Message text</param> /// <returns>Byte array containing formatted syslog message</returns> private byte[] BuildSyslogMessage5424(LogEventInfo logEvent, SyslogFacility facility, SyslogSeverity severity, string body) { // Calculate PRI field var priority = CalculatePriorityValue(facility, severity).ToString(CultureInfo.InvariantCulture); var version = ProtocolVersion.ToString(CultureInfo.InvariantCulture); var time = logEvent.TimeStamp.ToString("o"); // Get sender machine name var machine = Left(MachineName.Render(logEvent), 255); var sender = Left(Sender.Render(logEvent), 48); var procId = Left(ProcId.Render(logEvent), 128); var msgId = Left(MsgId.Render(logEvent), 32); var headerData = Encoding.ASCII.GetBytes($"<{priority}>{version} {time} {machine} {sender} {procId} {msgId} "); var structuredData = Encoding.UTF8.GetBytes(StructuredData.Render(logEvent) + " "); var messageData = Encoding.UTF8.GetBytes(body); var allData = new List <byte>(headerData.Length + structuredData.Length + _bom.Length + messageData.Length); allData.AddRange(headerData); allData.AddRange(structuredData); allData.AddRange(_bom); allData.AddRange(messageData); return(allData.ToArray()); }
/// <summary>Builds rfc-3164 compatible message</summary> /// <param name="logEvent">The NLog.LogEventInfo</param> /// <param name="facility">Syslog Facility to transmit message from</param> /// <param name="severity">Syslog severity level</param> /// <param name="body">Message text</param> /// <returns>Byte array containing formatted syslog message</returns> private byte[] BuildSyslogMessage3164(LogEventInfo logEvent, SyslogFacility facility, SyslogSeverity severity, string body) { // Calculate PRI field var priority = CalculatePriorityValue(facility, severity).ToString(CultureInfo.InvariantCulture); var time = logEvent.TimeStamp.ToLocalTime().ToString(TimestampFormat, _usCulture); // Get sender machine name var machine = MachineName.Render(logEvent); // Get sender var sender = Sender.Render(logEvent); return(Encoding.ASCII.GetBytes($"<{priority}>{time} {machine} {sender}: {body}{Environment.NewLine}")); }
/// <summary>Builds a syslog-compatible message using the information we have available</summary> /// <param name="logEvent">The NLog.LogEventInfo</param> /// <param name="facility">Syslog Facility to transmit message from</param> /// <param name="priority">Syslog severity level</param> /// <param name="body">Message text</param> /// <returns>Byte array containing formatted syslog message</returns> private byte[] BuildSyslogMessage(LogEventInfo logEvent, SyslogFacility facility, SyslogSeverity priority, string body) { switch (Rfc) { case RfcNumber.Rfc5424: return(BuildSyslogMessage5424(logEvent, facility, priority, body)); default: return(BuildSyslogMessage3164(logEvent, facility, priority, body)); } }