Reputation: 63
I have used this config lots of times before and never had any problems so I stripped my project back to basics. I am using Specflow with Specrun and the specrun part is the only thing which has changed.
I am trying to configure a serilog logger using the appsettings.json file which is located in my.net core bin base directory. \bin\Debug\netcoreapp3.1
Previously I could just reference the appsettings without a path without a basepath but this has changed when I moved from NUnit to Specflow. Still using basepath can still find the location. However when I attempt to create the logger and use readfrom it is referencing the appsettings file from a weird location and not the ConfigurationRoot in the configuration variable I just set.
Below are the two setup variables. The first for the configuration root of appsettings the second for the serilog logger. The first is successful and gives me what looks to be a valid IConfiguration object. The second is where the error occurs. This error being "System.IO.FileNotFoundException: 'Could not find file '\bin\Debug\netcoreapp3.1\SpecFlowPlusRunner\netcoreapp3.1\TechTalk.SpecRun.Framework.Executor.anycpu.netcoreapp3_1.deps.json'.'"
var configuration = new ConfigurationBuilder()
.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Some additional information of note. This is a netcore 3.1 app The code is executed within a specflow class packages included are.
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.6" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
<PackageReference Include="SpecFlow" Version="3.3.57" />
<PackageReference Include="SpecFlow.NetCore" Version="1.3.5" />
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.3.57" />
<PackageReference Include="SpecRun.SpecFlow" Version="3.3.41" />
Upvotes: 0
Views: 875
Reputation: 5835
I assume that your basepath for the configuration is wrong. Assembly.GetExecutingAssembly()
is probably returning one of the SpecFlow+ Runner assemblies, which are contained in the SpecFlowPlusRunner
subfolder.
To get the folder where your test assemblies are, we have an API for it: TestRunContext.TestDirectory
(https://docs.specflow.org/projects/specflow-runner/en/latest/Usage/SpecFlow-Runner-APIs.html#string-testdirectory-get).
With that, you should get the correct folder where your appsettings.json is.
Full disclosure: I am one of the developers of SpecFlow and SpecFlow+.
Upvotes: 0