public static byte[] Serialize(this ISyslogMessageSerializer serializer, SysLog.Message message) { byte[] datagramBytes; using (var stream = new MemoryStream()) { serializer.Serialize(message, stream); stream.Position = 0; datagramBytes = new byte[stream.Length]; stream.Read(datagramBytes, 0, (int)stream.Length); } return(datagramBytes); }
private static void LogToSyslog(Level level, String message) { if ((syslogSender == null) || (syslogSerializer == null)) { return; } SysLog.Severity severity; switch (level) { case Level.All: case Level.Debug: severity = SysLog.Severity.Debug; break; case Level.Trace: severity = SysLog.Severity.Informational; break; case Level.Info: severity = SysLog.Severity.Notice; break; case Level.Warning: severity = SysLog.Severity.Warning; break; case Level.Error: severity = SysLog.Severity.Error; break; case Level.Fatal: severity = SysLog.Severity.Critical; break; default: return; } SysLog.Message syslogMessage = new SysLog.Message(DateTimeOffset.Now, syslogFacility, severity, syslogMachineName, syslogApplicationName, RemoveDiacritics(message)); try { syslogSender.Send(syslogMessage, syslogSerializer); } catch (Exception e) { LogToConsole(Level.Warning, String.Format("Failed to send to remote Syslog ({0})", e.Message)); } }
public void Serialize(SysLog.Message message, Stream stream) { var priorityValue = CalculatePriorityValue(message.Facility, message.Severity); string timestamp = null; if (message.DateTimeOffset.HasValue) { DateTimeOffset dt = message.DateTimeOffset.Value; timestamp = String.Format("{0} {1} {2}", Months[dt.Month - 1], dt.Day < 10 ? " " + dt.Day : dt.Day.ToString(), dt.ToString("HH:mm:ss")); } var headerBuilder = new StringBuilder(); headerBuilder.Append("<").Append(priorityValue).Append(">"); headerBuilder.Append(timestamp).Append(" "); headerBuilder.Append(message.HostName).Append(" "); headerBuilder.Append(message.AppName).Append(": "); headerBuilder.Append(message.Data ?? ""); byte[] asciiBytes = Encoding.ASCII.GetBytes(headerBuilder.ToString()); stream.Write(asciiBytes, 0, asciiBytes.Length); }
private static void LogToSyslog(Level level, String message) { if ((syslogSender == null) || (syslogSerializer == null)) return; SysLog.Severity severity; switch (level) { case Level.All : case Level.Debug : severity = SysLog.Severity.Debug; break; case Level.Trace : severity = SysLog.Severity.Informational; break; case Level.Info : severity = SysLog.Severity.Notice; break; case Level.Warning : severity = SysLog.Severity.Warning; break; case Level.Error : severity = SysLog.Severity.Error; break; case Level.Fatal : severity = SysLog.Severity.Critical; break; default : return; } SysLog.Message syslogMessage = new SysLog.Message(DateTimeOffset.Now, syslogFacility, severity, syslogMachineName, syslogApplicationName, RemoveDiacritics(message)); try { syslogSender.Send(syslogMessage, syslogSerializer); } catch { } }
public void Send(SysLog.Message message, ISyslogMessageSerializer serializer) { byte[] datagramBytes = serializer.Serialize(message); udpClient.Send(datagramBytes, datagramBytes.Length); }
public void Serialize(SysLog.Message message, Stream stream) { var priorityValue = CalculatePriorityValue(message.Facility, message.Severity); // Note: The .Net ISO 8601 "o" format string uses 7 decimal places for fractional second. Syslog spec only allows 6, hence the custom format string var timestamp = message.DateTimeOffset.HasValue ? message.DateTimeOffset.Value.ToString("yyyy-MM-ddTHH:mm:ss.ffffffK") : null; var messageBuilder = new StringBuilder(); messageBuilder.Append("<").Append(priorityValue).Append(">"); messageBuilder.Append(message.Version); messageBuilder.Append(" ").Append(timestamp.FormatSyslogField(NilValue)); messageBuilder.Append(" ").Append(message.HostName.FormatSyslogAsciiField(NilValue, 255, asciiCharsBuffer)); messageBuilder.Append(" ").Append(message.AppName.FormatSyslogAsciiField(NilValue, 48, asciiCharsBuffer)); messageBuilder.Append(" ").Append(message.ProcId.FormatSyslogAsciiField(NilValue, 128, asciiCharsBuffer)); messageBuilder.Append(" ").Append(message.MsgId.FormatSyslogAsciiField(NilValue, 32, asciiCharsBuffer)); writeStream(stream, Encoding.ASCII, messageBuilder.ToString()); // Structured data foreach (SysLog.StructuredDataElement sdElement in message.StructuredDataElements) { messageBuilder.Clear() .Append(" ") .Append("[") .Append(sdElement.SdId.FormatSyslogSdnameField(asciiCharsBuffer)); writeStream(stream, Encoding.ASCII, messageBuilder.ToString()); foreach (System.Collections.Generic.KeyValuePair <string, string> sdParam in sdElement.Parameters) { messageBuilder.Clear() .Append(" ") .Append(sdParam.Key.FormatSyslogSdnameField(asciiCharsBuffer)) .Append("=") .Append("\"") .Append( sdParam.Value != null ? sdParam.Value .Replace("\\", "\\\\") .Replace("\"", "\\\"") .Replace("]", "\\]") : String.Empty ) .Append("\""); writeStream(stream, Encoding.UTF8, messageBuilder.ToString()); } // ] stream.WriteByte(93); } if (!String.IsNullOrWhiteSpace(message.Data)) { // Space stream.WriteByte(32); stream.Write(Encoding.UTF8.GetPreamble(), 0, Encoding.UTF8.GetPreamble().Length); writeStream(stream, Encoding.UTF8, message.Data); } }