Skip to content
/ Log Public

Simple and yet powerful logging library with performance and extensibility in mind.

License

Notifications You must be signed in to change notification settings

najlot/Log

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Najlot.Log

Build status NuGet Version alert_status sqale_rating reliability_rating security_rating sqale_index code_smells duplicated_lines_density vulnerabilities ncloc coverage

Current state of the master branch is ALPHA for version 4.0 - there will be a lot of changes following.

Please see branch v_3.1.4 for a stable version


Getting started:

You can start with just two lines of code:

var log = LogAdministrator.Instance.AddConsoleDestination().GetLogger(typeof(Program));
log.Info("Hello, World");

You can set the logging level on initialization and may change it later:

LogAdministrator.Instance
	.AddConsoleDestination(useColors: true)
	.SetLogLevel(LogLevel.Trace);

var log = LogAdministrator.Instance.GetLogger(typeof(Program));

log.Info("Hello, World!");

LogAdministrator.Instance.SetLogLevel(LogLevel.Warn);
log.Info("This message will not be logged.");

You tell the logger to log asynchronous (Putting the writing on an other thread):

LogAdministrator.Instance
    .SetCollectMiddleware<ConcurrentCollectMiddleware, FileDestination>()
    .SetCollectMiddleware<SyncCollectMiddleware, ConsoleDestination>()
    .AddConsoleDestination(useColors: true)
    .AddFileDestination("log.txt");

When you need something, then there is an example of a lot of things that are implemented:

LogConfigurationMapper.Instance.AddToMapping<ConsoleFormatMiddleware>();
LogConfigurationMapper.Instance.AddToMapping<DebugDestination>();

LogAdministrator.Instance
	// Using synchronous or asynchronous middleware to collect messages.
	// You can not use both. The last one called gets applied.
	//.SetCollectMiddleware<SyncCollectMiddleware, FileDestination>()
	.SetCollectMiddleware<ConcurrentCollectMiddleware, FileDestination>()

	// Tell the logger to write to a file and
	// calculate the file path it should write to.
	.AddFileDestination("{Year}-{Month}-{Day}.log")

	// Write to console using custom formatting and applying colors for different loglevel
	.AddMiddleware<ConsoleFormatMiddleware, ConsoleDestination>()
	.AddConsoleDestination(useColors: true)
				
	// Add a destination implemented below.
	.AddCustomDestination(new DebugDestination())

	// Read configuration from an XML file,
	// write an example file when not found,
	// listen for and apply changes to all loggers,
	// without restarting your application.
	.ReadConfigurationFromXmlFile("Najlot.Log.config",
		listenForChanges: true,
		writeExampleIfSourceDoesNotExists: true);

// Take specific logger.
var log = LogAdministrator.Instance.GetLogger(typeof(Program));

// Begin a scope of work
using (log.BeginScope("start up"))
{
	log.Trace("Beginning to start...");

	try
	{
		throw new Exception();
	}
	catch (Exception ex)
	{
		// Log a fatal error with the exception that caused the error.
		log.Fatal(ex, "Exception test: ");
	}
}

log.Info("Press any key.");
log.Flush();
Console.Read();

// Middleware used to format the output for console.
[LogConfigurationName(nameof(ConsoleFormatMiddleware))]
public sealed class ConsoleFormatMiddleware : IMiddleware
{
	public IMiddleware NextMiddleware { get; set; }

	public void Execute(IEnumerable<LogMessage> messages)
	{
		foreach (var message in messages)
		{
			message.Message = $"{message.DateTime} {LogArgumentsParser.InsertArguments(message.RawMessage, message.Arguments)} {message.Exception}";
		}

		NextMiddleware.Execute(messages);
	}

	public void Flush()
	{
	}

	public void Dispose()
	{
	}
}

// Custom destination that writes to System.Diagnostics.Debug.
[LogConfigurationName(nameof(DebugDestination))]
public sealed class DebugDestination : IDestination
{
	public void Log(IEnumerable<LogMessage> messages)
	{
		foreach (var message in messages)
		{
			System.Diagnostics.Debug.WriteLine(message.Message);
		}
	}

	public void Flush()
	{
	}

	public void Dispose()
	{
	}
}

Najlot.Log has a provider for Microsoft.Extensions.Logging:

using Microsoft.Extensions.Logging;
using Najlot.Log.Extensions.Logging;
using LogLevel = Najlot.Log.LogLevel;

...

var loggerFactory = new LoggerFactory();

loggerFactory.AddNajlotLog(administrator =>
{
	administrator
		.SetLogLevel(LogLevel.Info)
		.AddConsoleDestination(true)
		.AddFileDestination("log.txt");
});

var logger = loggerFactory.CreateLogger("default");
logger.LogInformation("Hello, World!");

About

Simple and yet powerful logging library with performance and extensibility in mind.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages