-
-
Notifications
You must be signed in to change notification settings - Fork 133
Update Logging.md #220
base: gh-pages
Are you sure you want to change the base?
Update Logging.md #220
Conversation
Additional info for enabling logging when writing unit tests
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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
This is possibly becoming the wrong place for this, but.... I do see 3 methods: I have tried ` var logger = new LoggerConfiguration()
and also neither of these seem to work. If you can help me figure out how to get IdentityServer to hook up the SeriLog then I may be able to contribute this code to the repo, as people may want to fire up a unit test project like this. Again, may be now becoming the wrong place to post this - should I open a new issue? In any case I'm still unable to get to the root of my actual problem (as much as this is fun) |
Quoting our docs
And one of our samples: That's all that is needed. Are we making it too easy? ;) |
@leastprivilege It would seem so, for me at least :D But jokes aside - what you're suggesting IS really easy but it's not working for me. I can only assume it's because it's a unit test so I thought I could set up an in-memory OWIN server and wire up the logger manually to Serilog and that would solve it, but I can't seem to get the logger wired up. Every chance it's me missing something, because I'm open minded about my capacity for stupidity :D |
@damianh Anything special you need to do in liblog for unit tests? |
So can you guarantee that the code that sets up the static serilog logger gets executed in the context of the test? Which test framework are you using? Also - have you ever tried to get logging working outside of unit tests? And once that is working - transfer that to you unit test scenario? |
No, in the unit test the static logger doesn't get created, only when I add that code to the unit test. I'm using NUnit for testing. It does seem like setting up OWIN test server in the unit test project should solve this issue though? When doing so the code is fired because the log file is created, but it seems IdentityServer doesn't wire up the logger to Serilog |
Also the owin test host must be created per test. That's the way it works. Assume every test runs in a fresh environment. I think that's the cause of all your problems. Sent from my iPad
|
I have no idea why this editor is modifying my code but the TestServer.Create also specifies OwinTestConfiguration which is what sets up my middleware where I am creating the logger `using (var server = TestServer.Create())
}` Which is creating a test server only for the lifetime of that test, and this doesn't work |
@leastprivilege Sorry for slow reply mate...
Yes if you are using xunit2 because of I don't know if nunit has a similar concern (parallel testing) or not? @richardterris which version of serilog are you using? |
Hi @damianh thanks for the response. Serliog version 2.0.0 is what I'm using Thanks again! |
Additional info for enabling logging when writing unit tests