问题描述
我创建了一个使用者/作业,它将以C#语言在Linux上作为一个进程运行.
I created a consumer/job that I will have running as a process on linux written in C#.
该过程将:
- 从RabbitMQ阅读消息
- 对数据库进行更改
- 记录所有错误
nlog上有关.net核心的所有文档都在aspnet核心上.当我尝试获取ILogger
实现时,它返回null.
All the documentation on nlog about .net core are on aspnet core. When I try to get an ILogger
implementation, it returns null.
这里是接线和用法的例外:
Here is an except of wiring and usage:
static void ConfigureServices()
{
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true);
var services = new ServiceCollection();
Configuration = builder.Build();
[...]
services.AddLogging();
ServiceProvider = services.BuildServiceProvider();
var loggerFactory = ServiceProvider.GetService<ILoggerFactory>();
loggerFactory.AddNLog();
}
static void Main(string[] args)
{
ConfigureServices();
var logger = ServiceProvider.GetService<NLog.ILogger>();
logger.Debug("Logging");
[...]
}
不要与环境变量ASPNETCORE_ENVIRONMENT
混淆;它仅用于确定要使用哪个appsettings.json
.
Do not be confused with the environment variable ASPNETCORE_ENVIRONMENT
; it is used solely to determine which appsettings.json
to use.
我的代码基于此问题报告.
最后,这些是我目前已安装的软件包.
Finally, these are the packages I currently have installed.
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
<PackageReference Include="NLog" Version="5.0.0-beta09" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta5" />
<PackageReference Include="Npgsql" Version="3.2.4.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0" />
</ItemGroup>
推荐答案
.NET Core 1控制台应用程序中基于NLog的完整简约示例(基于 NLog.Extensions.Logging 存储库):
A complete minimalistic example of NLog in a .NET Core 1 console app (based on NLog.Extensions.Logging repository):
var services = new ServiceCollection();
services.AddLogging();
var provider = services.BuildServiceProvider();
var factory = provider.GetService<ILoggerFactory>();
factory.AddNLog();
factory.ConfigureNLog("nlog.config");
var logger = provider.GetService<ILogger<Program>>();
logger.LogCritical("hello nlog");
参考文献:
<ItemGroup>
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
</ItemGroup>
nlog.config:
nlog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
internalLogFile="internal-nlog.txt">
<variable name="Layout"
value="${longdate}|${level:uppercase=true}|${logger}|${message}"/>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File"
name="allfile"
fileName="nlog-all-${shortdate}.log"
layout="${Layout}" />
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
</rules>
</nlog>
这篇关于将NLog接线并注入到.Net Core控制台应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!