Skip to content

Commit

Permalink
syslog clinet added ... debug starting
Browse files Browse the repository at this point in the history
  • Loading branch information
DBJDBJ committed Oct 22, 2024
1 parent 35752c5 commit 4469168
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dbjcore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
Expand Down
203 changes: 203 additions & 0 deletions src/dbjsyslog/Syslog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
/*
public class Program
{
public static void Main(string[] args)
{
// Initialize first
SyslogClient.Initialize("syslog.example.com");
// Then you can use it in three ways:
// 1. Using the Instance property
SyslogClient.Instance.SendMessage("Using instance directly");
// 2. Using static helper methods
SyslogClient.Log("Using static helper");
// 3. Using dependency injection (recommended)
var syslogClient = SyslogClient.Instance;
syslogClient.SendMessage("Using injected instance");
}
}
*/


namespace dbjcore
{

public class Syslog : IDisposable
{
private readonly string _hostname;
private readonly int _port;
private UdpClient? _udpClient;
private bool _disposed;
private static Syslog ? _instance;
private static readonly object _lock = new object();

public static Syslog Instance
{
get
{
if (_instance == null)
{
throw new InvalidOperationException("Syslog not initialized. Call Initialize first.");
}
return _instance;
}
}
/*
* yes I know ... needs to be called
*/
public static Syslog Initialize(string hostname = "localhost", int port = 514)
{
lock (_lock)
{
if (_instance != null)
{
throw new InvalidOperationException("SyslogClient already initialized.");
}
_instance = new Syslog(hostname, port);
AppDomain.CurrentDomain.ProcessExit += (s, e) => _instance.Dispose();
// If using ASP.NET Core, you can also use:
// applicationLifetime.ApplicationStopping.Register(() => _instance.Dispose());
return _instance;
}
}

// Example static method for direct usage without getting Instance
public static void Log(string message, SyslogSeverity severity = SyslogSeverity.Informational)
{
Instance.SendMessage(message, severity);
}

private Syslog(string hostname, int port = 514)
{
_hostname = hostname;
_port = port;
_udpClient = new UdpClient();
}

public void SendMessage(string message, SyslogSeverity severity = SyslogSeverity.Informational, SyslogFacility facility = SyslogFacility.User)
{
ThrowIfDisposed();
int priority = ((int)facility * 8) + (int)severity;
string timestamp = DateTime.Now.ToString("MMM dd HH:mm:ss");
string hostname = Dns.GetHostName();

string syslogMessage = $"<{priority}>{timestamp} {hostname} {message}";
byte[] messageBytes = Encoding.ASCII.GetBytes(syslogMessage);

_udpClient!.Send(messageBytes, messageBytes.Length, _hostname, _port);
}

public void SendMessage(string format, object[] args, SyslogSeverity severity = SyslogSeverity.Informational, SyslogFacility facility = SyslogFacility.User)
{
ThrowIfDisposed();
string message = string.Format(format, args);
SendMessage(message, severity, facility);
}

public async Task SendMessageAsync(string message, SyslogSeverity severity = SyslogSeverity.Informational, SyslogFacility facility = SyslogFacility.User)
{
ThrowIfDisposed();
int priority = ((int)facility * 8) + (int)severity;
string timestamp = DateTime.Now.ToString("MMM dd HH:mm:ss");
string hostname = Dns.GetHostName();

string syslogMessage = $"<{priority}>{timestamp} {hostname} {message}";
byte[] messageBytes = Encoding.ASCII.GetBytes(syslogMessage);

await _udpClient!.SendAsync(messageBytes, messageBytes.Length, _hostname, _port);
}

public async Task SendMessageAsync(string format, object[] args, SyslogSeverity severity = SyslogSeverity.Informational, SyslogFacility facility = SyslogFacility.User)
{
ThrowIfDisposed();
string message = string.Format(format, args);
await SendMessageAsync(message, severity, facility);
}

private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(Syslog));
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_udpClient?.Close();
_udpClient?.Dispose();
if (_udpClient is not null)
_udpClient = null;
}
_disposed = true;
}
}

~Syslog()
{
Dispose(false);
}
}

// Enums remain the same...

public enum SyslogSeverity
{
Emergency = 0,
Alert = 1,
Critical = 2,
Error = 3,
Warning = 4,
Notice = 5,
Informational = 6,
Debug = 7
}

public enum SyslogFacility
{
Kernel = 0,
User = 1,
Mail = 2,
System = 3,
Security = 4,
Syslog = 5,
Printer = 6,
Network = 7,
UUCP = 8,
Clock = 9,
Security2 = 10,
FTP = 11,
NTP = 12,
LogAudit = 13,
LogAlert = 14,
Clock2 = 15,
Local0 = 16,
Local1 = 17,
Local2 = 18,
Local3 = 19,
Local4 = 20,
Local5 = 21,
Local6 = 22,
Local7 = 23
}
}

0 comments on commit 4469168

Please sign in to comment.