This is a repeated question but don't know what happens in this solution scenario , We are setting up an WorkerService
in .Net core 3.1
. For logging we are using NLog
. While building we were getting the error:
System.IO.FileNotFoundException: 'Could not load file or assembly 'NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c'. The system cannot find the file specified.'
This error occues in Project.cs(shown in code below). The ServiceFileLogger belongs to another Library Class.(folder struncture is shown below)
Project.cs
public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseWindowsService() .ConfigureServices((hostContext, services) => { IConfiguration config = hostContext.Configuration; services.AddSingleton<IConfiguration>(config); services.AddSingleton<ServiceSettings>(config.GetSection("Application").Get<ServiceSettings>());// Error comes here during the injection. services.AddTransient<ServiceFileLogger>(_logger=> new ServiceFileLogger("DATASOURCEMONITOR", config.GetSection("Application:LogLevel").ToString())); services.AddHostedService<Worker>(); }); }}
We even changed the Nlog version 4.9.2
to NLog version 4.9.0
()More Info
<Project Sdk="Microsoft.NET.Sdk.Worker"><PropertyGroup><TargetFramework>netcoreapp3.1</TargetFramework><UserSecretsId>dotnet-WorkerServiceLearn-817A165A-A227-4F73-ABBC-EE79E10DE8A3</UserSecretsId><ApplicationIcon /><OutputType>Exe</OutputType><StartupObject /></PropertyGroup><ItemGroup><PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.5" /><PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.5" /><PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" /></ItemGroup><ItemGroup><Reference Include="WorkerServiceLearn.Library"><HintPath>..\WorkerServiceLearn.Library\bin\Debug\netcoreapp3.1\WorkerServiceLearn.Library.dll</HintPath></Reference></ItemGroup></Project>
Don't know what to change here, kindly help us and provide some documents to validate.
Repository Link is here, kindly go through it and advice us.