You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by da...@apache.org on 2022/08/21 13:57:33 UTC

[logging-log4net] branch master updated: Extension point for handling new lines in RemoteSysLogAppender added

This is an automated email from the ASF dual-hosted git repository.

davydm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4net.git


The following commit(s) were added to refs/heads/master by this push:
     new 091b652f Extension point for handling new lines in RemoteSysLogAppender added
     new cd7b6a67 Merge pull request #93 from komsa-ag/Feature/RemoteSyslogAppenderCustomization
091b652f is described below

commit 091b652f913c2fa4ce942374f932c0879d6acbb1
Author: Jan Friedrich <Ja...@komsa.de>
AuthorDate: Mon Aug 8 16:04:32 2022 +0200

    Extension point for handling new lines in RemoteSysLogAppender added
---
 src/log4net/Appender/RemoteSyslogAppender.cs | 166 ++++++++++++++-------------
 1 file changed, 88 insertions(+), 78 deletions(-)

diff --git a/src/log4net/Appender/RemoteSyslogAppender.cs b/src/log4net/Appender/RemoteSyslogAppender.cs
index 675d6f97..b67abd32 100644
--- a/src/log4net/Appender/RemoteSyslogAppender.cs
+++ b/src/log4net/Appender/RemoteSyslogAppender.cs
@@ -344,88 +344,98 @@ namespace log4net.Appender
 		/// </remarks>
 		protected override void Append(LoggingEvent loggingEvent)
 		{
-            try
-            {
-                // Priority
-                int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
-
-                // Identity
-                string identity;
-
-                if (m_identity != null)
-                {
-                    identity = m_identity.Format(loggingEvent);
-                }
-                else
-                {
-                    identity = loggingEvent.Domain;
-                }
-
-                // Message. The message goes after the tag/identity
-                string message = RenderLoggingEvent(loggingEvent);
-
-                Byte[] buffer;
-                int i = 0;
-                char c;
-
-                StringBuilder builder = new StringBuilder();
-
-                while (i < message.Length)
-                {
-                    // Clear StringBuilder
-                    builder.Length = 0;
-
-                    // Write priority
-                    builder.Append('<');
-                    builder.Append(priority);
-                    builder.Append('>');
-
-                    // Write identity
-                    builder.Append(identity);
-                    builder.Append(": ");
-
-                    for (; i < message.Length; i++)
-                    {
-                        c = message[i];
-
-                        // Accept only visible ASCII characters and space. See RFC 3164 section 4.1.3
-                        if (((int)c >= 32) && ((int)c <= 126))
-                        {
-                            builder.Append(c);
-                        }
-                        // If character is newline, break and send the current line
-                        else if ((c == '\r') || (c == '\n'))
-                        {
-                            // Check the next character to handle \r\n or \n\r
-                            if ((message.Length > i + 1) && ((message[i + 1] == '\r') || (message[i + 1] == '\n')))
-                            {
-                                i++;
-                            }
-                            i++;
-                            break;
-                        }
-                    }
-
-                    // Grab as a byte array
-                    buffer = this.Encoding.GetBytes(builder.ToString());
+			try
+			{
+				// Priority
+				int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
+
+				// Identity
+				string identity;
+
+				if (m_identity != null)
+				{
+					identity = m_identity.Format(loggingEvent);
+				}
+				else
+				{
+					identity = loggingEvent.Domain;
+				}
+
+				// Message. The message goes after the tag/identity
+				string message = RenderLoggingEvent(loggingEvent);
+
+				byte[] buffer;
+				int i = 0;
+
+				StringBuilder builder = new StringBuilder();
+
+				while (i < message.Length)
+				{
+					// Clear StringBuilder
+					builder.Length = 0;
+
+					// Write priority
+					builder.Append('<');
+					builder.Append(priority);
+					builder.Append('>');
+
+					// Write identity
+					builder.Append(identity);
+					builder.Append(": ");
+
+					AppendMessage(message, ref i, builder);
+
+					// Grab as a byte array
+					buffer = this.Encoding.GetBytes(builder.ToString());
 
 #if NET_4_5 || NETSTANDARD
-                    Client.SendAsync(buffer, buffer.Length, RemoteEndPoint).Wait();
+					Client.SendAsync(buffer, buffer.Length, RemoteEndPoint).Wait();
 #else
-                    this.Client.Send(buffer, buffer.Length, this.RemoteEndPoint);
+					this.Client.Send(buffer, buffer.Length, this.RemoteEndPoint);
 #endif
-                }
-            }
-            catch (Exception e)
-            {
-                ErrorHandler.Error(
-                    "Unable to send logging event to remote syslog " +
-                    this.RemoteAddress.ToString() +
-                    " on port " +
-                    this.RemotePort + ".",
-                    e,
-                    ErrorCode.WriteFailure);
-            }
+				}
+			}
+			catch (Exception e)
+			{
+				ErrorHandler.Error(
+						"Unable to send logging event to remote syslog " +
+						this.RemoteAddress.ToString() +
+						" on port " +
+						this.RemotePort + ".",
+						e,
+						ErrorCode.WriteFailure);
+			}
+		}
+
+		/// <summary>
+		/// Appends the rendered message to the buffer
+		/// </summary>
+		/// <param name="message">rendered message</param>
+		/// <param name="characterIndex">index of the current character in the message</param>
+		/// <param name="builder">buffer</param>
+		protected virtual void AppendMessage(string message, ref int characterIndex, StringBuilder builder)
+		{
+			for (; characterIndex < message.Length; characterIndex++)
+			{
+				char c = message[characterIndex];
+
+				// Accept only visible ASCII characters and space. See RFC 3164 section 4.1.3
+				if (((int)c >= 32) && ((int)c <= 126))
+				{
+					builder.Append(c);
+				}
+				// If character is newline, break and send the current line
+				else if ((c == '\r') || (c == '\n'))
+				{
+					// Check the next character to handle \r\n or \n\r
+					if ((message.Length > characterIndex + 1) && ((message[characterIndex + 1] == '\r') || (message[characterIndex + 1] == '\n')))
+					{
+						characterIndex++;
+					}
+					characterIndex++;
+					break;
+				}
+			}
 		}
 
 		/// <summary>