Serilog logging in test (xunit) projects
Hi, is there any example or ABP support for configure serilog logger ?
Install the Serilog.Extensions.Logging nuget package and add the following code to the ConfigureServices method of the module class :
Log.Logger = new LoggerConfiguration()
.WriteTo.XXX( ) // Select the output location you need
.CreateLogger();
context.Services.AddLogging(c => c.AddSerilog());
Then you can inject the ILogger <> interface into your test class.
PS: Maybe using ITestOutputHelper in xunit project is a better choice, see https://xunit.net/docs/capturing-output
@liangshiw thanks for your answere. But it is not working.
If i put code you provide into ConfigureServices then this code is executed after DI try to inject ILogger into test constructor. At that time ILogger is not register jet.
I try to execute ServiceProvider.GetService<ILogger>() from test method (not CTOR), but this returns null. So ILogger is not registered right.
My test project derives from AbpIntegratedTest<TStartModule>. Is this OK ?
@sebitsi Inject the ILogger<> , like ILogger<SampleAppServiceTests>
@liangshiw Sorry still not working. :(
I think i mixed something. I have this inheritance:
SQLConstants : CommonTestBase : SharedTestBase<CommonTestModule>
SharedTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule>
I put your suggested code into CommonTestModule.ConfigureServices. Is this right ?
If i inject ILogger<SQLConstants> into SQLConstants's CTOR as you suggested then debuging of test project is stopped withou error.
On the other hand calling ServiceProvider.GetService<ILogger<SQLConstants>>() is working.
i got logger instance and i can log into it (file).
I also noticed if i raise expection this exception is not logged.
@sebitsi, can you transfer the logger implementation codes to Program.cs file in your project.
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.XXX( ) // Select the output location you need
.CreateLogger();
}
And then do not forget to add UseSerilog() at the end of CreateHostBuilder method.
Example:
internal static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseAutofac()
.UseSerilog();
@cotur What do you mean ? I have no problem using serilog in ASP.NET Core project. Just with test project.
Hi @sebitsi,
This answer ( https://github.com/abpframework/abp/issues/3012#issuecomment-596129597 ) is for UI test. For xUnit tests, there is no ABP support for serilog integration.
Maybet it will help your problem : Output logs to Xunit using Serilog Static Logger
Or you can do it manually. Just create string list and add what you want to log. Then you can write your logs to where you want.
@cotur There is no problem with manually write to log file. I can do this with yours and @liangshiw suggestions. Thanks for that.
But I expect some logging support if exception raised in abp modules when test is executed.
What about that ?