EonaCat.Logger 1.3.4

Prefix Reserved
This package has a SemVer 2.0.0 package version: 1.3.4+8d902100de.20241014.
dotnet add package EonaCat.Logger --version 1.3.4                
NuGet\Install-Package EonaCat.Logger -Version 1.3.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EonaCat.Logger" Version="1.3.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EonaCat.Logger --version 1.3.4                
#r "nuget: EonaCat.Logger, 1.3.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EonaCat.Logger as a Cake Addin
#addin nuget:?package=EonaCat.Logger&version=1.3.4

// Install EonaCat.Logger as a Cake Tool
#tool nuget:?package=EonaCat.Logger&version=1.3.4                

EonaCat.Logger

Log application information to log files.

Be sure the following dependencies are added:

Microsoft.Extensions.Logging.Abstractions Microsoft.Extensions.DependencyInjection.Abstractions Microsoft.Extensions.DependencyInjection Microsoft.Extensions.Logging

Easy way of logging without configuring anything:

LogManager.Instance.WriteAsync("INFO", ELogType.INFO, true);
LogManager.Instance.WriteAsync("WARNING", ELogType.WARNING, true);
LogManager.Instance.WriteAsync("ERROR", ELogType.ERROR, true);
LogManager.Instance.WriteAsync("DEBUG", ELogType.DEBUG, true);
LogManager.Instance.WriteAsync("CRITICAL", ELogType.CRITICAL, true);
LogManager.Instance.WriteAsync("TRACE", ELogType.TRACE, true);
LogManager.Instance.WriteAsync("TRAFFIC", ELogType.TRAFFIC, true);
LogManager.Instance.WriteAsync("NONE", ELogType.NONE, true);

**Logging in .NET 4.8 or higher: **

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            // Add services to the container.
            FileLoggerOptions options = new FileLoggerOptions();
            options.MaxRolloverFiles = 20;
            options.FileSizeLimit = 1 * 1024 * 1024 / 4;
            options.UseLocalTime = true;

            var builder = Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).ConfigureLogging(x => x.AddEonaCatFileLogger(fileLoggerOptions: options, filenamePrefix: "web"));
            return builder;
        }
    }    

Example of advanced logging class:

// Import necessary namespaces.
using EonaCat.Logger;
using EonaCat.Logger.Managers;
using EonaCat.Logger.Syslog;
using EonaCat.Logger.SplunkServer;
using EonaCat.Logger.GrayLog;

namespace EonaCat.Logger.Advanced
{
    // The GrayLogSettings class represents settings for GrayLog servers.
    internal class GrayLogSettings
    {
        // Constructor for GrayLogSettings class.
        public GrayLogSettings(string facility, string source, string version = "1.1")
        {
            Facility = facility;
            Source = source;
            Version = version;
        }

        // Facility represents the facility for GrayLog logs.
        public string Facility { get; }
        // Source represents the source for GrayLog logs.
        public string Source { get; }
        // Version represents the version for GrayLog logs (default is "1.1").
        public string Version { get; }
    }

    // The Logger class provides advanced logging functionality.
    internal class Logger
    {
        private static readonly LogManager _logManager;
        private static ConsoleLogTextWriter _consoleLogTextWriter;

        // Exposes a TextWriter property to allow for logging to the console.
        public static TextWriter ConsoleLog { get; internal set; }

        // Static constructor for the Logger class.
        static Logger()
        {
            // Create and configure a LogManager for logging.
            _logManager = new LogManager(new LoggerSettings { RemoveMessagePrefix = true });
            _logManager.OnException += _logManager_OnException;
            _logManager.OnLogLevelDisabled += _logManager_OnLogLevelDisabled;
            _logManager.Settings.FileLoggerOptions.FileNamePrefix = "advanced";
            _logManager.Settings.UseLocalTime = true;
            _logManager.Settings.FileLoggerOptions.UseLocalTime = true;
            _logManager.Settings.SysLogServers = new List<SyslogServer>();
            _logManager.Settings.SplunkServers = new List<SplunkServer>();
            _logManager.Settings.GrayLogServers = new List<GrayLogServer>();
            _logManager.StartNewLogAsync();
        }

        // Event handler for LogManager exceptions, writes messages to the console.
        private static void _logManager_OnException(object? sender, ErrorMessage e)
        {
            Console.WriteLine(e.Message);
            if (e.Exception != null)
            {
                Console.WriteLine(e.Exception);
            }
        }

         // Event handler for LogManager loglevel disabled notifications, writes messages to the console.
        private static void _logManager_OnLogLevelDisabled(object? sender, ErrorMessage e)
        {
            Console.WriteLine(e.Message);
            if (e.Exception != null)
            {
                Console.WriteLine(e.Exception);
            }
        }

        // Redirects console output to the log.
        public static void RedirectConsoleToLog()
        {
            // Create the callback function to capture console output and log it.
            static void callback(string line) => Info(line, writeToConsole: false);

            _consoleLogTextWriter = new ConsoleLogTextWriter(callback);
            Console.SetOut(_consoleLogTextWriter);
        }

        // Adds a GrayLog server with the specified hostname and port.
        public static void AddGrayLogServer(string hostname, int port)
        {
            _logManager.Settings.GrayLogServers.Add(new GrayLogServer(hostname, port));
        }

        // Removes a GrayLog server from the configuration.
        public static bool RemoveGrayLogServer(GrayLogServer grayLogServer)
        {
            return _logManager.Settings.GrayLogServers.Remove(grayLogServer);
        }

        // Sets the state for sending logs to GrayLog servers.
        public static void GrayLogState(bool state)
        {
            _logManager.Settings.SendToGrayLogServers = state;
        }

        // Adds a Splunk server with the specified HEC URL and token.
        public static void AddSplunkServer(string splunkHecUrl, string splunkHecToken, bool disableSSL = false)
        {
            var splunkServer = new SplunkServer(splunkHecUrl, splunkHecToken);
            if (disableSSL)
            {
                splunkServer.DisableSSLValidation();
            }
            _logManager.Settings.SplunkServers.Add(splunkServer);
        }

        // Removes a Splunk server from the configuration.
        public static bool RemoveSplunkServer(SplunkServer splunkServer)
        {
            return _logManager.Settings.SplunkServers.Remove(splunkServer);
        }

        // Sets the state for sending logs to Splunk servers.
        public static void SplunkState(bool state)
        {
            _logManager.Settings.SendToSplunkServers = state;
        }

        // Adds a Syslog server with the specified IP address and port.
        public static void AddSyslogServer(string ipAddress, int port)
        {
            _logManager.Settings.SysLogServers.Add(new SyslogServer(ipAddress, port));
        }

        // Removes a Syslog server from the configuration.
        public static bool RemoveSyslogServer(SyslogServer syslogServer)
        {
            return _logManager.Settings.SysLogServers.Remove(syslogServer);
        }

        // Sets the state for sending logs to Syslog servers.
        public static void SysLogState(bool state)
        {
            _logManager.Settings.SendToSyslogServers = state;
        }

        // Logs an informational message.
        public static void Info(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            WriteAsync(message, ELogType.INFO, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings);
        }

        // Internal method to write logs.
        private static void WriteAsync(string message, ELogType logType, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            if (grayLogSettings != null)
            {
                // Log the message with specified settings.
                _logManager.WriteAsync(message, logType, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings.Facility, grayLogSettings.Source, grayLogSettings.Version);
            }
            else
            {
                // Log the message with default settings.
                _logManager.WriteAsync(message, logType, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers);
            }
        }

        // Logs a debug message.
        public static void Debug(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            WriteAsync(message, ELogType.DEBUG, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings);
        }

        // Logs an error message along with an exception.
        public static void Error(Exception exception, string message = null, bool isCriticalException = false, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            if (grayLogSettings != null)
            {
                // Log the exception and message with specified settings.
                _logManager.WriteAsync(exception, message, criticalException: isCriticalException, writeToConsole: writeToConsole, sendToSysLogServers: sendToSysLogServers, sendToSplunkServers: sendToSplunkServers, customSplunkSourceType: customSplunkSourceType, sendToGrayLogServers: sendToGrayLogServers, grayLogFacility: grayLogSettings.Facility, grayLogSource: grayLogSettings.Source, grayLogVersion: grayLogSettings.Version);
            }
            else
            {
                // Log the exception and message with default settings.
                _logManager.WriteAsync(exception, message, criticalException: isCriticalException, writeToConsole: writeToConsole, sendToSysLogServers: sendToSysLogServers, sendToSplunkServers: sendToSplunkServers, customSplunkSourceType: customSplunkSourceType, sendToGrayLogServers: sendToGrayLogServers);
            }
        }

        // Logs an error message.
        public static void Error(string message = null, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, string grayLogFacility = null, string grayLogSource = null, string grayLogVersion = "1.1", bool isCriticalException = false)
        {
            if (isCriticalException)
            {
                // Log a critical error message.
                _logManager.WriteAsync(message, ELogType.CRITICAL, writeToConsole);
            }
            else
            {
                // Log a regular error message.
                _logManager.WriteAsync(message, ELogType.ERROR, writeToConsole);
            }
        }

        // Logs a warning message.
        public static void Warning(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            WriteAsync(message, ELogType.WARNING, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings);
        }

        // Logs a critical message.
        public static void Critical(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            WriteAsync(message, ELogType.CRITICAL, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings);
        }

        // Logs a traffic message.
        public static void Traffic(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            WriteAsync(message, ELogType.TRAFFIC, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings);
        }

        // Logs a trace message.
        public static void Trace(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null)
        {
            WriteAsync(message, ELogType.TRACE, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings);
        }
    }
}

Code for enabling keyword masking (default: false):

Logger.LoggerSettings.UseMask = true;

Sensitive information will be replaced with MASKED You can add additional masking keywords using the MaskedKeywords property on the LoggerSettings object

You can also override the MaskSensitiveInformation method of the batchLoggingProvider to add your own masking

Default maskings (when the masking is enabled): IP addresses MAC addresses Emails Passwords Credit card numbers Social security numbers (SSN) and BSN (Dutch Citizen Service Number) API keys/tokens Phone numbers (generic and Dutch specific) Dates of birth (DOB) or other date formats IBAN/Bank account numbers (generic and Dutch specific) JWT tokens URLs with sensitive query strings License keys Public and private keys (e.g., PEM format) Dutch KVK number (8 or 12 digits) Dutch BTW-nummer (VAT number) Dutch driving license number (10-12 characters) Dutch health insurance number (Zorgnummer) Other Dutch Bank Account numbers (9-10 digits) Dutch Passport Numbers (9 alphanumeric characters Dutch Identification Document Numbers (varying formats) Custom keywords specified in LoggerSettings

Code for enabling GrayLog in the above advanced logger class:

 // Set grayLog information
 var ipAddress = "192.168.10.50"
 var port = 9000;

Logger.AddGrayLogServer(ipAddress, port);
Logger.GrayLogState(true);

Code for enabling Splunk in the above advanced logger class:

// Set Splunk information
var splunkHecUrl = "https://192.168.10.51:8088/services/collector";
var splunkHecToken = "cf0c7e68-e14b-48c1-949f-efdf5d388254";

Logger.AddSplunkServer(splunkHecUrl, splunkHecToken, true);
Logger.SplunkState(true);

Code for enabling SysLog in the above advanced logger class:

 // Set sysLog information
 var ipAddress = "192.168.10.53";
 var port = 514;

Logger.AddSyslogServer(ipAddress, port);
Logger.SysLogState(true);

Logging Test with default LogManager:

Task.Run(() => 
{
    var loggerSettings = new LoggerSettings();
    loggerSettings.UseLocalTime = true;
    loggerSettings.FileLoggerOptions.UseLocalTime = true;
    loggerSettings.MaxLogType = ELogType.TRACE;
    loggerSettings.FileLoggerOptions.FileSizeLimit = 1024 * 1024 * 1;
    loggerSettings.FileLoggerOptions.FileNamePrefix = "AllTypes";
    loggerSettings.FileLoggerOptions.MaxRolloverFiles = 5;
	
	// Create an instance of the logManager with the specified loggerSettings
    var logger = new LogManager(loggerSettings);

    // 10.000.000 lines to go :) 
    for (int i = 0; i < 10000000; i++)
    {
        logger.WriteAsync($"test to file {i} INFO", ELogType.INFO);
        logger.WriteAsync($"test to file {i} CRITICAL", ELogType.CRITICAL);
        logger.WriteAsync($"test to file {i} DEBUG", ELogType.DEBUG);
        logger.WriteAsync($"test to file {i} ERROR", ELogType.ERROR);
        logger.WriteAsync($"test to file {i} TRACE", ELogType.TRACE);
        logger.WriteAsync($"test to file {i} TRAFFIC", ELogType.TRAFFIC);
        logger.WriteAsync($"test to file {i} WARNING", ELogType.WARNING);
        logger.WriteAsync($"test to file {i} NONE", ELogType.NONE);
        Console.WriteLine($"Logged: {i}");
        Task.Delay(1);
    }
});

How to retrieve exceptions thrown by the library (from splunk, syslog and grayLog)

LogManager logManager = new LogManager();
logManager.OnException += LogManager_OnException;

private void LogHelper_OnException(object sender, ErrorMessage e)
{
    if (e.Exception != null)
	{
       Console.WriteLine(e.Exception);
	}
	
	if (e.Message != null)
	{
	    Console.WriteLine("Error message from logger: " + e.Message)
	}
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on EonaCat.Logger:

Package Downloads
EonaCat.Dns

This is a Dns Server

EonaCat.Blocky

Blocky is the Dns GUI for EonaCat.Dns Blocking domains the way you want it.

EonaCat.Network

EonaCat Networking library with Quic, TCP, UDP, WebSockets and a Webserver

EonaCat.SysLog

EonaCatSysLog Library

EonaCat.SqlChanges

EonaCatSqlChanges is a SQL Server service broker change library created for .NET Standard.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.4 66 10/14/2024
1.3.2 69 8/15/2024

Public release version