gpt4 book ai didi

c# - 连接 NLog 并将其注入(inject) .NET Core 控制台应用程序

转载 作者:可可西里 更新时间:2023-11-01 08:08:41 27 4
gpt4 key购买 nike

我创建了一个消费者/工作,我将在 Linux 上以 C# 编写的进程运行。

该过程将:

  1. 从 RabbitMQ 读取消息
  2. 修改数据库
  3. 记录任何错误

NLog 上所有关于 .NET Core 的文档都在 ASP.NET Core 上。当我尝试获取 ILogger 实现时,它返回 null。

这里是接线和使用的异常(exception)情况:

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

我的代码基于这个 issue report .

最后,这些是我目前已经安装的包。

<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 存储库):

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:

<?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>

关于c# - 连接 NLog 并将其注入(inject) .NET Core 控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com