Documentation
Documentation copied to clipboard
Update Logging.md
Additional info for enabling logging when writing unit tests
This is possibly becoming the wrong place for this, but.... I have now gone as far as set up OWIN TestServer within my unit test project to try and get an in-memory OWIN pipeline. Within the Startup I am creating the SeriLog logger and trying to tie that to the IdentityServer Logging, but I can't see how to do that because I don't see any such static Log property.
I do see 3 methods: public static ILog GetCurrentClassLogger(); public static ILog GetLogger(Type type); public static ILog GetLogger(string name);
I have tried ` var logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.File(@"C:\idsLogs.txt") .CreateLogger();
LoggingOptions options = new LoggingOptions();
options.EnableHttpLogging = true;
var idserverLogger = LogProvider.GetLogger(logger.GetType().Name);`
and also var idserverLogger = LogProvider.GetLogger(logger.GetType());
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
Liblog picks up any of the following logging libraries automatically. There is no IdentityServer3 specific configuration required - you just need to configure one of the above logging frameworks in your host.
And one of our samples: https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/WebHost%20(minimal)/WebHost/Startup.cs#L16
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. The code will create the log file but nothing is getting written to it; it should be because my unit test is returning an internal server error.
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 would have thought the startup class would still be hit when the unit test calls tokenClient.RequestClientCredentialsAsync but I guess not.
I'm using NUnit for testing. I haven't as yet tried to do something outside of the unit tests but I can do later when I finish work for the day.
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
On 15 Aug 2016, at 12:02, Richard Terris [email protected] wrote:
No, in the unit test the static logger doesn't get created, only when I add that code to the unit test. I would have thought the startup class would still be hit when the unit test calls tokenClient.RequestClientCredentialsAsync but I guess not.
I'm using NUnit for testing. I haven't as yet tried to do something outside of the unit tests but I can do later when I finish work for the day.
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
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
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 The code in my unit test is:
`using (var server = TestServer.Create<OwinTestConfiguration>()) { var tokenResponse = tokenClient.RequestClientCredentialsAsync("applicationLogin").Result;
if (tokenResponse.IsError)
{
Console.Write(tokenResponse.Error);
}
Console.Write("Access token: " + TokenHelper.DecodeAndWrite(tokenResponse.AccessToken));
}`
Which is creating a test server only for the lifetime of that test, and this doesn't work
@leastprivilege Sorry for slow reply mate...
@damianh Anything special you need to do in liblog for unit tests?
Yes if you are using xunit2 because of ITestoutputHelper
. http://dhickey.ie/2015/06/capturing-log-output-in-tests-with-xunit2/
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. From what I've read, it doesn't look like NUnit supports parallelisation, but I don't think it should be an issue here because I just have (at the moment) a single unit test and I can't get logging wired up.
Serliog version 2.0.0 is what I'm using
Thanks again!