问题
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.
回答1:
You have to install NLog also by using the Nuget Package Manager or running the below command in the package manager console.
Install-Package NLog -Version 4.7.2
Reference: https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3#1-add-dependency-in-csproj-manually-or-using-nuget
回答2:
NLog works for me like this:
In the WorkerServiceLearn project:
Add Nuget package : Nlog
Add Nuget package : NLog.Extensions.Logging
Add a "nlog.config" file
Declare NLog service in the code:
return Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { var config = hostContext.Configuration; services.AddLogging(loggingBuilder => { // Microsoft.Extensions.Logging; loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug); // NLog.Extensions.Logging loggingBuilder.AddNLog(config); });
In the WorkerServiceLearnLibrary project:
- Remove NLog.Web.AspNetCore (I personally don't use it)
- Add Nuget package : NLog
来源:https://stackoverflow.com/questions/62594712/unable-to-load-assembly-in-worker-service-net-core3-1-and-nlog-4-9-2