Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
70 changes: 64 additions & 6 deletions src/log4net/Appender/RemoteSyslogAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
using log4net.Util;
using log4net.Layout;
using System.Text;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Concurrent;

namespace log4net.Appender;

Expand Down Expand Up @@ -367,7 +371,8 @@
// Grab as a byte array
byte[] buffer = Encoding.GetBytes(builder.ToString());

Client.SendAsync(buffer, buffer.Length, RemoteEndPoint).Wait();
//Client.SendAsync(buffer, buffer.Length, RemoteEndPoint).Wait();
_sendQueue.Add(buffer);
}
}
catch (Exception e) when (!e.IsFatal())
Expand Down Expand Up @@ -412,7 +417,7 @@
}
}

/// <summary>

Check warning on line 420 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

XML comment is not placed on a valid language element

Check warning on line 420 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

XML comment is not placed on a valid language element

Check warning on line 420 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

XML comment is not placed on a valid language element

Check warning on line 420 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

XML comment is not placed on a valid language element

Check warning on line 420 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

XML comment is not placed on a valid language element

Check warning on line 420 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

XML comment is not placed on a valid language element
/// Initialize the options for this appender
/// </summary>
/// <remarks>
Expand All @@ -420,11 +425,11 @@
/// Initialize the level to syslog severity mappings set on this appender.
/// </para>
/// </remarks>
public override void ActivateOptions()
{
base.ActivateOptions();
_levelMapping.ActivateOptions();
}
//public override void ActivateOptions()
//{
// base.ActivateOptions();
// _levelMapping.ActivateOptions();
//}

/// <summary>
/// Translates a log4net level to a syslog severity.
Expand Down Expand Up @@ -531,4 +536,57 @@
/// </remarks>
public SyslogSeverity Severity { get; set; }
}
private readonly BlockingCollection<byte[]> _sendQueue = new();
private CancellationTokenSource? _cts;
private Task? _pumpTask;
public override void ActivateOptions()

Check warning on line 542 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.ActivateOptions()'

Check warning on line 542 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.ActivateOptions()'

Check warning on line 542 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.ActivateOptions()'

Check warning on line 542 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.ActivateOptions()'

Check warning on line 542 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.ActivateOptions()'

Check warning on line 542 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.ActivateOptions()'
{
base.ActivateOptions();
// Start the background pump
_cts = new CancellationTokenSource();
_pumpTask = Task.Run(() => ProcessQueueAsync(_cts.Token), CancellationToken.None);
}

protected override void OnClose()

Check warning on line 550 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.OnClose()'

Check warning on line 550 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.OnClose()'

Check warning on line 550 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.OnClose()'

Check warning on line 550 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.OnClose()'

Check warning on line 550 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.OnClose()'

Check warning on line 550 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'RemoteSyslogAppender.OnClose()'
{
// Signal shutdown and wait for the pump to drain
_cts?.Cancel();
_pumpTask?.Wait(TimeSpan.FromSeconds(5)); // or your own timeout
base.OnClose();
}

private async Task ProcessQueueAsync(CancellationToken token)
{
// We create our own UdpClient here, so that client lifetime is tied to this task
using (var udp = new UdpClient())
{
udp.Connect(RemoteAddress?.ToString(), RemotePort);

try
{
while (!token.IsCancellationRequested)
{
// Take next message or throw when cancelled
byte[] datagram = _sendQueue.Take(token);
try
{
await udp.SendAsync(datagram, datagram.Length);

Check warning on line 573 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)

Check warning on line 573 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)

Check warning on line 573 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
}
catch (Exception ex) when (!ex.IsFatal())
{
ErrorHandler.Error("RemoteSyslogAppender: send failed", ex, ErrorCode.WriteFailure);
}
}
}
catch (OperationCanceledException)
{
// Clean shutdown: drain remaining items if desired
while (_sendQueue.TryTake(out var leftover))
{
try { await udp.SendAsync(leftover, leftover.Length); }

Check warning on line 586 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)

Check warning on line 586 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)

Check warning on line 586 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
catch { /* ignore */ }

Check warning on line 587 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Modify 'ProcessQueueAsync' to catch a more specific allowed exception type, or rethrow the exception (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1031)

Check warning on line 587 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

Modify 'ProcessQueueAsync' to catch a more specific allowed exception type, or rethrow the exception (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1031)

Check warning on line 587 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Modify 'ProcessQueueAsync' to catch a more specific allowed exception type, or rethrow the exception (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1031)

Check warning on line 587 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Modify 'ProcessQueueAsync' to catch a more specific allowed exception type, or rethrow the exception (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1031)

Check warning on line 587 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Modify 'ProcessQueueAsync' to catch a more specific allowed exception type, or rethrow the exception (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1031)

Check warning on line 587 in src/log4net/Appender/RemoteSyslogAppender.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Modify 'ProcessQueueAsync' to catch a more specific allowed exception type, or rethrow the exception (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1031)
}
}
}
}
}