How do I enable logging in Thinktecture IdentityServer v3?
Asked Answered
C

4

10

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.

Centrifuge answered 15/4, 2015 at 9:12 Comment(0)
E
5

or simply read the documentation:

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

Expecting answered 16/4, 2015 at 5:18 Comment(1)
It's kinda funny when you tell the author of the lib, 'Good find.' :)Casein
C
9

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.

Crematorium answered 15/4, 2015 at 9:36 Comment(5)
Thanks Michal. I've elaborated some more on your answer.Centrifuge
Where is 'LogProvider from ? When I type 'LogProvider' and a 'dot' I see 'CreateLogProvider'. Not 'SetCurrentLogProvider'. I've installed 'LibLog'Allodial
@Mohan My answer is specific for IdentityServer v3 that comes from before 1 year. You may be using another version.Ilo
It is IdentityServer v3.Allodial
I'm getting the same - there is only a noop provider in the logging namespace and no packages relevant from what I can see?Architect
A
6

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.

Agribusiness answered 10/11, 2015 at 9:44 Comment(1)
Sorry for commenting on this old answer. To make it work on the ASP.NET Core 2.0 project, i did the following 1) Added the above code to the constructor of the Startup class. 2) Added additional parameter ILoggerFactory logFactory to the Configure method in the startup class. 3) Within the Configure method added this line logFactory.AddSerilog();Zimmermann
E
5

or simply read the documentation:

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

Expecting answered 16/4, 2015 at 5:18 Comment(1)
It's kinda funny when you tell the author of the lib, 'Good find.' :)Casein
D
0

There's a log4net gotcha where for some reason it doesn't write to a new log file. My startup.cs literally includes this:

Log.Debug("starting log.  do not remove this line.");
LogProvider.GetLogger(typeof(Startup)).Log(LogLevel.Debug, () => "starting up");

I have no idea why, I just know that I tore out a bunch of hair before this made it work.

Deodar answered 16/10, 2015 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.