Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable logging in Thinktecture IdentityServer v3?

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.

like image 411
Biscuits Avatar asked Apr 15 '15 09:04

Biscuits


3 Answers

or simply read the documentation:

https://identityserver.github.io/Documentation/docsv2/configuration/logging.html

like image 144
leastprivilege Avatar answered Nov 01 '22 10:11

leastprivilege


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.

like image 32
Michał Komorowski Avatar answered Nov 01 '22 10:11

Michał Komorowski


Following the documentation of last release HERE you can just set Log.Logger in your Startup.cs.

For example using Serilog (install it via Nuget by searching Serilog) you can just set up log to a file by adding this line of code in the Configuration method of the Startup class

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug() // change with your desired log level
            .WriteTo.File(@"C:\myPath.txt") // remember to assign proper writing privileges on the file
            .CreateLogger();

For more options see the documentation link above.

like image 41
Naigel Avatar answered Nov 01 '22 10:11

Naigel