Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ish-1313/SyslogCore

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SyslogCore (Enhanced)

Modern syslog client for .NET with dual logging (local libc + remote UDP) and RFC 5424 support. Fork of jitbit/SyslogCore with critical improvements.

Usage

// 1. One-time initialization
cSyslogClient.Init(appName: "MyApp",remoteServer: "10.0.0.1" // Optional);

// 2. Logging (auto module detection)
cSyslogClient.Write("Payment processed", SyslogLevel.Info);

// 3. Graceful shutdown
cSyslogClient.Shutdown();

Key Features

  • Dual logging: Local (libc) + Remote (UDP RFC 5424)

  • Structured messages: Timestamps, hostnames, priority levels

  • Auto caller detection: [CallerMemberName] integration

  • Resource-safe: Proper GCHandle and UdpClient disposal

  • Zero dependencies: Single C# file (~150 LOC)

Why This Fork?

The original library was limited to local syslog. We added:

🚀 Remote syslog server support

📡 RFC 5424 compliance

🛡️ Safer resource management

🔍 Automatic caller module tracking

Usage example

ASP.NET Core Integration

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

cSyslogClient.Init("WebApp", "192.168.113.100");

app.Lifetime.ApplicationStopping.Register(() => 
{
    cSyslogClient.Write("Shutting down...");
    cSyslogClient.Shutdown();
});

app.MapGet("/", () => {
    cSyslogClient.Write("Homepage visited"); // Auto-detects "[GET] /" as module 
    return "Hello World";
});

Configuration Options

cSyslogClient.Init(
    appName: "Backend",
    remoteServer: "10.0.0.1",  // UDP target
    remotePort: 8514,          // Custom port
    customHostName: "server-1" // Override hostname
);

Expected output

2025-06-25T13:31:41.190245+03:00 dev69-u lk[206841]: [INFO] main: startup v0.2.1.0
2025-06-25T13:50:40.203339+03:00 dev69-u be[207439]: [INFO] main: startup v2.0.0.0
2025-06-25T13:57:50.985781+03:00 dev69-u be[207439]: [INFO] main(): shutdown
2025-06-25T13:57:51.513765+03:00 dev69-u be[207668]: [INFO] main: startup v2.0.0.0
2025-06-25T13:58:16.690086+03:00 dev69-u lk[206841]: [INFO] lk-files: Test from filesController
2025-06-25T13:58:23.113230+03:00 dev69-u be[207668]: [INFO] GetFileName: File report.pdf uploaded to caller

Logging to a Separate File (local0-local7)

To direct logs to a dedicated file instead of /var/log/syslog, use Linux's local facilities (local0 through local7). Example for local0: Create rsyslog config (/etc/rsyslog.d/20-myapp.conf):

# Logs with facility=local0 to /var/log/myapp.log
local0.*    /var/log/myapp.log
& stop  # Prevent duplicate logging

This code uses Local0 by default. You can change it in:

// In LibC.openlog() call:
LibC.openlog(..., facility: 16 << 3);  // 16=local0, 17=local1, etc.

Performance Notes

For high-throughput scenarios:

  • Consider batching UDP messages

  • Future upgrade path to LibraryImport (AOT support)

  • Async logging possible via _udpClient.SendAsync

Info

  • Original credits: Jitbit
  • Enhancements by: ish-1313
  • License: MIT (same as original)

About

Simple (!) way to write to syslog aka /dev/log aka /var/log/syslog in .NET Core on Linux

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%