Kentor authservices and log4net

814 Views Asked by At

Is there a way to use log4net as Kentor Authservices Logger? Documentation states that "Connect an ILoggerAdapter to your SPOptions.Logger. If you are using the OWIN middleware this is done for you automatically and you can see the output in the OWIN/Katana logging.", but I don't really understand what does it mean.

1

There are 1 best solutions below

1
On BEST ANSWER

You would write an adapter between the log4net logger and the Kentor.AuthServices logger, something like this:

public class log4netLoggerAdapter : Kentor.AuthServices.ILoggerAdapter
{
    private log4net.ILog _logger;

    public log4netLoggerAdapter(log4net.ILog logger)
    {
        _logger = logger;
    }

    public void WriteError(string message, Exception ex)
    {
        _logger.Error(message, ex);
    }

    public void WriteInformation(string message)
    {
        _logger.Info(message);
    }

    public void WriteVerbose(string message)
    {
        _logger.Debug(message);
    }
}

And then assign an instance of that to your AuthServices Options.SPOptions.Logger. For example, in the SampleMVCApplication Global.asax.cs, adding a line in Application_Start:

Kentor.AuthServices.Mvc.AuthServicesController.Options.SPOptions.Logger =
            new log4netLoggerAdapter(log4net.LogManager.GetLogger("AuthServices"));

This last part would of course be different depending on which module you are using and how you are loading your configuration, but the key is the assignment of an ILoggerAdapter to your SPOptions.Logger