Skip to content

Commit 091b652

Browse files
Jan FriedrichJan Friedrich
authored andcommitted
Extension point for handling new lines in RemoteSysLogAppender added
1 parent e26e1dd commit 091b652

File tree

1 file changed

+88
-78
lines changed

1 file changed

+88
-78
lines changed

src/log4net/Appender/RemoteSyslogAppender.cs

Lines changed: 88 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -344,88 +344,98 @@ public void AddMapping(LevelSeverity mapping)
344344
/// </remarks>
345345
protected override void Append(LoggingEvent loggingEvent)
346346
{
347-
try
348-
{
349-
// Priority
350-
int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
351-
352-
// Identity
353-
string identity;
354-
355-
if (m_identity != null)
356-
{
357-
identity = m_identity.Format(loggingEvent);
358-
}
359-
else
360-
{
361-
identity = loggingEvent.Domain;
362-
}
363-
364-
// Message. The message goes after the tag/identity
365-
string message = RenderLoggingEvent(loggingEvent);
366-
367-
Byte[] buffer;
368-
int i = 0;
369-
char c;
370-
371-
StringBuilder builder = new StringBuilder();
372-
373-
while (i < message.Length)
374-
{
375-
// Clear StringBuilder
376-
builder.Length = 0;
377-
378-
// Write priority
379-
builder.Append('<');
380-
builder.Append(priority);
381-
builder.Append('>');
382-
383-
// Write identity
384-
builder.Append(identity);
385-
builder.Append(": ");
386-
387-
for (; i < message.Length; i++)
388-
{
389-
c = message[i];
390-
391-
// Accept only visible ASCII characters and space. See RFC 3164 section 4.1.3
392-
if (((int)c >= 32) && ((int)c <= 126))
393-
{
394-
builder.Append(c);
395-
}
396-
// If character is newline, break and send the current line
397-
else if ((c == '\r') || (c == '\n'))
398-
{
399-
// Check the next character to handle \r\n or \n\r
400-
if ((message.Length > i + 1) && ((message[i + 1] == '\r') || (message[i + 1] == '\n')))
401-
{
402-
i++;
403-
}
404-
i++;
405-
break;
406-
}
407-
}
408-
409-
// Grab as a byte array
410-
buffer = this.Encoding.GetBytes(builder.ToString());
347+
try
348+
{
349+
// Priority
350+
int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
351+
352+
// Identity
353+
string identity;
354+
355+
if (m_identity != null)
356+
{
357+
identity = m_identity.Format(loggingEvent);
358+
}
359+
else
360+
{
361+
identity = loggingEvent.Domain;
362+
}
363+
364+
// Message. The message goes after the tag/identity
365+
string message = RenderLoggingEvent(loggingEvent);
366+
367+
byte[] buffer;
368+
int i = 0;
369+
370+
StringBuilder builder = new StringBuilder();
371+
372+
while (i < message.Length)
373+
{
374+
// Clear StringBuilder
375+
builder.Length = 0;
376+
377+
// Write priority
378+
builder.Append('<');
379+
builder.Append(priority);
380+
builder.Append('>');
381+
382+
// Write identity
383+
builder.Append(identity);
384+
builder.Append(": ");
385+
386+
AppendMessage(message, ref i, builder);
387+
388+
// Grab as a byte array
389+
buffer = this.Encoding.GetBytes(builder.ToString());
411390

412391
#if NET_4_5 || NETSTANDARD
413-
Client.SendAsync(buffer, buffer.Length, RemoteEndPoint).Wait();
392+
Client.SendAsync(buffer, buffer.Length, RemoteEndPoint).Wait();
414393
#else
415-
this.Client.Send(buffer, buffer.Length, this.RemoteEndPoint);
394+
this.Client.Send(buffer, buffer.Length, this.RemoteEndPoint);
416395
#endif
417-
}
418-
}
419-
catch (Exception e)
420-
{
421-
ErrorHandler.Error(
422-
"Unable to send logging event to remote syslog " +
423-
this.RemoteAddress.ToString() +
424-
" on port " +
425-
this.RemotePort + ".",
426-
e,
427-
ErrorCode.WriteFailure);
428-
}
396+
}
397+
}
398+
catch (Exception e)
399+
{
400+
ErrorHandler.Error(
401+
"Unable to send logging event to remote syslog " +
402+
this.RemoteAddress.ToString() +
403+
" on port " +
404+
this.RemotePort + ".",
405+
e,
406+
ErrorCode.WriteFailure);
407+
}
408+
}
409+
410+
/// <summary>
411+
/// Appends the rendered message to the buffer
412+
/// </summary>
413+
/// <param name="message">rendered message</param>
414+
/// <param name="characterIndex">index of the current character in the message</param>
415+
/// <param name="builder">buffer</param>
416+
protected virtual void AppendMessage(string message, ref int characterIndex, StringBuilder builder)
417+
{
418+
for (; characterIndex < message.Length; characterIndex++)
419+
{
420+
char c = message[characterIndex];
421+
422+
// Accept only visible ASCII characters and space. See RFC 3164 section 4.1.3
423+
if (((int)c >= 32) && ((int)c <= 126))
424+
{
425+
builder.Append(c);
426+
}
427+
// If character is newline, break and send the current line
428+
else if ((c == '\r') || (c == '\n'))
429+
{
430+
// Check the next character to handle \r\n or \n\r
431+
if ((message.Length > characterIndex + 1) && ((message[characterIndex + 1] == '\r') || (message[characterIndex + 1] == '\n')))
432+
{
433+
characterIndex++;
434+
}
435+
characterIndex++;
436+
break;
437+
}
438+
}
429439
}
430440

431441
/// <summary>

0 commit comments

Comments
 (0)