Enabling OpenID Trace

OpenID 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 OpenID 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”: “Async”,
“Args”: {
“configure”: [
{
“Name”: “File”,
“Args”: {
“path”: “logs/openid-.log”,
“rollingInterval”: “Day”,
“retainedFileCountLimit”: 7,
“buffered”: true,
“flushToDiskInterval”: “00:00:01”
}
}
]
}
}
]
}



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


Serilog.AspNetCore
Serilog.Sinks.Async
Serilog.Sinks.File



The following example CreateWebHostBuilder method in the Program class removes the default logging providers and adds the Serilog provider.


public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(configureLogging => configureLogging.ClearProviders())
.UseSerilog((webHostBuilderContext, loggerConfiguration) =>
loggerConfiguration.ReadFrom.Configuration(webHostBuilderContext.Configuration))
.UseStartup();



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.





















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


Microsoft.Extensions.Logging.Log4Net.AspNetCore



The following example CreateWebHostBuilder method in the Program class removes the default logging providers and adds the Log4Net provider.


public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureLogging(configureLogging => configureLogging.ClearProviders());
webBuilder.UseStartup();
}).ConfigureLogging(builder =>
{
builder.SetMinimumLevel(LogLevel.Debug);
builder.AddLog4Net(“log4net.config”);
});



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=“<a href=“http://www.nlog-project.org/schemas/NLog.xsd” “=””><a href=“http://www.nlog-project.org/schemas/NLog.xsd” “=”“><a href=“http://www.nlog-project.org/schemas/NLog.xsd” “=””><a href=“http://www.nlog-project.org/schemas/NLog.xsd” “=”“><a href=“http://www.nlog-project.org/schemas/NLog.xsd” “=””><a href=“http://www.nlog-project.org/schemas/NLog.xsd” “=”“><a href=“http://www.nlog-project.org/schemas/NLog.xsd””>http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi=“<a href=“http://www.w3.org/2001/XMLSchema-instance” “=””><a href=“http://www.w3.org/2001/XMLSchema-instance” “=”“><a href=“http://www.w3.org/2001/XMLSchema-instance” “=””><a href=“http://www.w3.org/2001/XMLSchema-instance” “=”“><a href=“http://www.w3.org/2001/XMLSchema-instance” “=””><a href=“http://www.w3.org/2001/XMLSchema-instance” “=”“><a href=“http://www.w3.org/2001/XMLSchema-instance””>http://www.w3.org/2001/XMLSchema-instance"
autoReload=“true”>




<target xsi:type=“File” name=“logfile” fileName=“${aspnet-appbasepath}\logs${shortdate}.log”
layout=“${longdate}|${level}|${message} ${exception:format=tostring}” />








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


NLog
NLog.Web.AspNetCore



The following example code in the Program class removes the default logging providers and adds the NLog provider.


public static void Main(string[] args)
{
var logger = NLogBuilder.ConfigureNLog(“nlog.config”).GetCurrentClassLogger();

try
{
CreateHostBuilder(args).Build().Run();
}
finally
{
NLog.LogManager.Shutdown();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog();



For more information, please refer to the NLog documentation.
The Getting started with ASP.NET Core 5 article describes how to configure and enable NLog in an ASP.NET Core 5 application.
There are similar articles for other versions of ASP.NET Core.