Kentor authservices 和 log4net

Kentor authservices and log4net

有没有办法使用 log4net 作为 Kentor Authservices Logger?文档指出 "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.",但我真的不明白这是什么意思。

您可以在 log4net 记录器和 Kentor.AuthServices 记录器之间编写一个适配器,如下所示:

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);
    }
}

然后将其实例分配给您的 AuthServices Options.SPOptions.Logger。比如在SampleMVCApplication Global.asax.cs中,在Application_Start中添加一行:

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

最后一部分当然会有所不同,具体取决于您使用的模块以及加载配置的方式,但关键是将 ILoggerAdapter 分配给您的 SPOptions.Logger