Enabling SAML for ASP.NET Core Trace

SAML debug trace may be enabled to assist with tracking down issues. The standard ASP.NET Core logging API is used.

In our examples, we make use of Serilog. However, any logging provider, including Serilog, Log4Net and NLog, may be used to capture the logging information.

Regardless of the logging provider used, the “Debug” level must be specified for “ComponentSpace”.

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Information",
    "ComponentSpace": "Debug"
  }
}

However, it’s not recommended to enable SAML debug trace in production environments, unless for problem determination, as it may impact performance.

Serilog

The following is an example Serilog configuration in appsettings.json. Logs are written to a “logs” folder under the application’s root folder.

  "Serilog": {
    "MinimumLevel": {
      "Default": "Warning",
      "Override": {
        "ComponentSpace": "Debug"
      }
    },
    "WriteTo": [
      {
        "Name": "Debug"
      },
      {
        "Name": "File",
        "Args": {
          "path": "logs/saml-.log",
          "rollingInterval": "Day"
        }
      }
    ]
  },

To support Serilog, the following NuGet packages must be included in the application.

Serilog.AspNetCore

The following code in the Program class adds the Serilog provider.

builder.Host.UseSerilog((hostBuilderContext, loggerConfiguration) =>
    loggerConfiguration.ReadFrom.Configuration(hostBuilderContext.Configuration));

For more information, please refer to the Serilog documentation.

Log4Net

The following is an example Log4Net configuration in log4net.config. Logs are written to a “logs” folder under the application’s root folder.

<log4net>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logs/" />
    <datePattern value="'saml'-yyyyMMdd.'log'"/>
    <staticLogFileName value="false"/>  
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %5level %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFileAppender" />
  </root>
</log4net>

To support Log4Net, the following NuGet packages must be included in the application.

Microsoft.Extensions.Logging.Log4Net.AspNetCore

The following code in the Program class adds the Log4Net provider.

builder.Logging.AddLog4Net();

For more information, please refer to the Log4Net documentation.

NLog

The following is an example NLog configuration in nlog.config. Logs are written to a “logs” folder under the application’s root folder.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  autoReload="true">
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>
  <targets>
    <target xsi:type="File" name="logfile" 
      fileName="${aspnet-appbasepath}\logs\${shortdate}.log"
      layout="${longdate}|${level}|${message} ${exception:format=tostring}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="logfile" />
  </rules>
</nlog>

To support NLog, the following NuGet packages must be included in the application.

NLog.Web.AspNetCore

The following code in the Program class adds the Log4Net provider.

builder.Host.UseNLog();

For more information, please refer to the NLog documentation.