Skip to content
Tom Kuijsten edited this page Oct 6, 2016 · 1 revision

Support

Restup supports a plugin model for logging. The logging statements are added, but if you want to actually do anything with it you will have to overwrite the ILogFactory. This approach will let you use your favorite logging framework and us focus on the http functionality.

Basic sample

This sample implements a logger which redirects the output to the Debug console.

First, you create the actual logger:

public class DebugLogger : AbstractLogger
{
    protected override bool IsLogEnabled(LogLevel trace)
    {
        // Ignore level, log everything
        return true;
    }

    protected override void LogMessage(string message, LogLevel loggingLevel, Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"{loggingLevel}: {message}");
        System.Diagnostics.Debug.WriteLine($"{ex}");
    }

    protected override void LogMessage(string message, LogLevel loggingLevel, params object[] args)
    {
        System.Diagnostics.Debug.WriteLine($"{loggingLevel}: {(string.Format(message, args))}");
    }
}

Then, you create the factory which controls when to create a logger:

public class DebugLogFactory : ILogFactory
{
	private ILogger _debugLogger;

	public DebugLogFactory()
	{
		_debugLogger = new DebugLogger();
	}

	public void Dispose()
	{
		_debugLogger = null;
	}

	public ILogger GetLogger(string name)
	{
		return _debugLogger;
	}

	public ILogger GetLogger<T>()
	{
		return _debugLogger;
	}
}

Finally, you should tell Restup to use the new DebugLogFactory:

LogManager.SetLogFactory(new DebugLogFactory());
Clone this wiki locally