How do I enable logging in Thinktecture IdentityServer v3?

Biscuits picture Biscuits · Apr 15, 2015 · Viewed 12.9k times · Source

How do I enable logging in Thinktecture IdentityServer v3?

I'm currently getting a generic error page, saying "There was an unexpected error".

I was able to figure out that the generic error gets returned by the ErrorPageFilterAttribute that appears to resolve some implementation of ILog for logging the details that I'm after.

I suspect it is some concrete implementation of ILog that needs to be configured somehow.

Answer

Michał Komorowski picture Michał Komorowski · Apr 15, 2015

I'm not an expert but I know something about IdentityServer so I may be able to help. IdentityServer v3 supports a few logging providers, for example NLog, Log4Net or Serilog. You have to select which one you want to use and configure it.

To see a sample how to do it I suggest to download the following project IdentityServer3.Samples with samples from github. There, among others, you will find WebHost (minimal) project which uses NLog. WebHost (minimal) is an example which shows a basic (minimal) configuration of IdentityServer v3 with IIS.

Another project SelfHost (Minimal with Serilog) shows how to use Serilog for logging in the scenario when IdentityServer is hosted by a console applicaion (without IIS).

EDIT:

The Thinktecture.IdentityServer.Core.Logging namespace has several implementations of ILogProvider. Here are a couple of those.

Log4NetLogProvider, that uses log4net.

NLogLogProvider, that uses NLog.

DiagnosticsTraceLogProvider, that uses System.Diagnostics.Trace.

TraceSourceLogProvider, that uses System.Diagnostics.TraceSource.

Besides first installing the necessary package or referencing the necessary library for your desired Log Provider, you further need to set it to become the current Log Provider during startup, like this.

LogProvider.SetCurrentLogProvider(new DiagnosticsTraceLogProvider());

Make sure you continue to follow any steps that are required to configure the underlying package or library that your current Log Provider uses. For example, the following configuration can be used with the DiagnosticsTraceLogProvider:

<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="TextWriter"
             type="System.Diagnostics.TextWriterTraceListener"
             initializeData="Trace.log" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

EDIT 2

Since I wrote my answer some details have been changed. Now IdentityServer uses LibLog library and there you can find different implementations of ILogProvider.

The project Custom Grants (more customization) shows how to use LibLog.