mpaul31 / Topshelf.LibLog

LibLog extension for Topshelf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Topshelf.LibLog

This integration library adds support for LibLog to Topshelf. LibLog enables dependency free logging. It contains transparent builtin support for NLog, Log4Net, EntLib Logging and SeriLog or allows the user to define a custom provider. Works with .NET 4 and higher.

Usage

To hookup LibLog to Topshelf, just one line of code is needed when configuring Topshelf

HostFactory.New(x =>
{
    x.UseLibLog();
});

LibLog will automatically scan for the following frameworks, NLog, Log4Net, EntLib Logging and SeriLog. If it cannot find any of these frameworks, logging is automatically disabled.

Custom logging

If you want to implement custom logging, you need to create a custom log provider and set it before the Topshelf Host is configured.

Topshelf.LibLog.Logging.LogProvider.SetCurrentLogProvider(new CustomLogProvider());
public class CustomLogProvider : Topshelf.LibLog.Logging.ILogProvider
{
	public Topshelf.LibLog.Logging.ILog GetLogger(string name)
	{
		return new CustomLogger(name);
	}
}

The customlogger should implement Topshelf.LibLog.Logging.ILog

public class CustomLogger : Topshelf.LibLog.Logging.ILog
{
	public CustomLogger(string name)
	{
		....
	}
	
	public bool Log(Topshelf.LibLog.Logging.LogLevel logLevel, Func<string> messageFunc)
	{
		if (messageFunc != null)
		{
			// Call to your own custom methods
			Log(messageFunc());
		}
		return true;
	}

	public void Log<TException>(Topshelf.LibLog.Logging.LogLevel logLevel, Func<string> messageFunc, TException exception) where TException : Exception
	{
		// Call to your own custom methods
		Log(messageFunc(), exception);
	}
}

About

LibLog extension for Topshelf

License:MIT License


Languages

Language:C# 99.9%Language:Batchfile 0.1%