简体   繁体   English

如何使用 Sustainsys.Saml2 设置 NLog

[英]How to setup NLog with Sustainsys.Saml2

I have an ASP.Net Web Forms app where I just integrated Sustainsys.Saml2 library.我有一个 ASP.Net Web Forms 应用程序,我刚刚集成了Sustainsys.Saml2库。

I've never used any sort of logging mechanism and I'm trying to figure out how to add or create an ILoggerAdapter for the library stated on their troubleshooting page.我从未使用过任何类型的日志记录机制,我正在尝试弄清楚如何为其故障排除页面上所述的库添加或创建ILoggerAdapter

I've decided to use NLog (please feel free to recommend a different one) and either I'm not understanding this well, or am not using the right keyword to search for what I need/want, or their isn't a lot of documentation on it.我决定使用 NLog(请随意推荐一个不同的),或者我不太了解这一点,或者没有使用正确的关键字来搜索我需要/想要的东西,或者它们不是很多关于它的文档。

Currently, I'm using the HttpModule version of Sustainsys.Saml2 .目前,我正在使用Sustainsys.Saml2HttpModule版本。 Any other information available upon request.应要求提供的任何其他信息。

Any help would be great.任何帮助都会很棒。

Currently, I'm configuring the Sustainsys.Saml2 library through both web.config and the global.asax files.目前,我正在通过 web.config 和 global.asax 文件配置Sustainsys.Saml2库。 Here's the class my global.asax calls:这是我 global.asax 调用的 class :

public class Saml2Config {
    private static bool _alreadyInitialized;
    private static readonly object Lock = new object();

    public static void Initialize() {
        if (_alreadyInitialized) {
            return;
        }

        lock (Lock) {
            if (_alreadyInitialized) {
                return;
            }

            var domain = PageHelper.GetDomainURL(true);

            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.EntityId.Id = $"{domain}/federation/Saml2";
            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.ModulePath = "/federation/Saml2";
            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.ReturnUrl = new Uri($"{domain}/mybarry");
            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.PublicOrigin = new Uri($"{domain}");

            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.Logger = new NullLoggerAdapter();

            _alreadyInitialized = true;
        }
    }
}

The interface is pretty straightforward 界面非常简单

public interface ILoggerAdapter
{
    void WriteInformation(string message);

    void WriteError(string message, Exception ex);

    void WriteVerbose(string message);
}

I would implement it as follows:我将按如下方式实现它:

public class NLogAdapter : ILoggerAdapter
{
    private static Logger Logger = LogManager.GetLogger("Saml2");

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

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

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

And finally set it:最后设置它:

Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.Logger = new NLogAdapter();

The ILoggerAdapter contains methods for different loglevels. ILoggerAdapter包含用于不同日志级别的方法。 Make an adapter class that implements ILoggerAdapter and writes to NLog.制作一个实现ILoggerAdapter并写入 NLog 的适配器 class。 Then set SPOptions.Logger to an instance of your adapter class.然后将SPOptions.Logger设置为适配器 class 的实例。

If you want an example, you can check out the adapter for Asp.Net Core that logs to the Asp.Net Core logging system and is the default for the Sustainsys.Saml2.AspNetCore2 package: https://github.com/Sustainsys/Saml2/blob/master/Sustainsys.Saml2.AspNetCore2/AspNetCoreLoggerAdapter.cs如果您想要一个示例,您可以查看 Asp.Net Core 的适配器,该适配器记录到 Asp.Net Core 日志记录系统,并且是Sustainsys.Saml2.AspNetCore2 package 的默认适配器: https://github.com/Sustainsys/ Saml2/blob/master/Sustainsys.Saml2.AspNetCore2/AspNetCoreLoggerAdapter.cs

For the Sustainsys.Saml2.HttpModule library the default is the NullLoggerAdapter which simply discards any logs.对于Sustainsys.Saml2.HttpModule库,默认值为NullLoggerAdapter ,它只是丢弃任何日志。 Only reason to use it is to not have to nullcheck the Logger property everywhere it is used (that code was written before the ?. syntax was introduced.)使用它的唯一原因是不必在任何使用它的地方对 Logger 属性进行空检查(该代码是在引入?.语法之前编写的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM