Skip to content
This repository has been archived by the owner on Dec 14, 2017. It is now read-only.

Update Logging.md #220

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docsv2/configuration/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ For certain scenarios (e.g. production) you want to make sure that no logging ou
```csharp
LogProvider.SetCurrentLogProvider(new NoopLogProvider());
```
## Unit Testing
When writing unit tests, you may want to enable logging to help with debugging, as some errors aren't too detailed (such as getting a 500 error back as the Token Response, for example - this could be a number of things).
Assuming that your unit tests are in a separate project from your Statup class where you register and initialiase your logger, then you will find that logging won't work.
In order to make logging work you will have to wrap the code which makes requests to IdentityServer in a using statment such as:
```csharp
using (var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the Serilog logger, don't you need to assign it to the static Log instance? I can't tell from this how IdSvr knows about your logger variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very strange... Last night when I got this working, it produced a good amount of logs and the issue seemed to be that the certificate installed had a key length of 1024 bytes.

Now when I run this, I am getting a log file, but only with the line I add:
logger.Debug("test!");

To try and figure this out, I have commented out the logging setup code in my Startup class and recompiled the project, and the logger creation certainly seems to work with the code in this PR - I assumed what I was doing above was creating an in-memory logger which would live for the duration of my unit test only? Or am I misunderstanding something?

I'm really interested in contributing to this because it's a great project and I'm trying to become more skilled in security in general, but obviously I'm quite at the beginning with this.

I'll keep playing just now to see if I can figure this out and I'll update the PR when I do.
But of course, if I'm doing something wrong please tell me.

Richard

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't know too much of how Serilog works internally, but the way IdSvr "knows" about it is that it discovers it via the static Log property that serilog has. So for your unit tests, I'd expect this static property to be set somewhere at initialization time (maybe even statically, as opposed to per-test).

.WriteTo.File(@"C:\idsLogs.txt")
.CreateLogger())
{
var tokenResponse = tokenClient.RequestClientCredentialsAsync("applicationLogin").Result;
}
```