CSoft.LoggerNLog
11.0.0
dotnet add package CSoft.LoggerNLog --version 11.0.0
NuGet\Install-Package CSoft.LoggerNLog -Version 11.0.0
<PackageReference Include="CSoft.LoggerNLog" Version="11.0.0" />
<PackageVersion Include="CSoft.LoggerNLog" Version="11.0.0" />
<PackageReference Include="CSoft.LoggerNLog" />
paket add CSoft.LoggerNLog --version 11.0.0
#r "nuget: CSoft.LoggerNLog, 11.0.0"
#:package CSoft.LoggerNLog@11.0.0
#addin nuget:?package=CSoft.LoggerNLog&version=11.0.0
#tool nuget:?package=CSoft.LoggerNLog&version=11.0.0
CSoft.LoggerNLog
通过 NLog 实现 CSoft.LogInterface 的 ILogFactory 和 ILogger。使用 NLog 的规则配置日志级别、目标和布局。
安装
dotnet add package CSoft.LoggerNLog --version 11.0.0
支持 .NET Standard 2.0 和 .NET 8、9、10、11。包包含各目标框架的 XML API 文档。
写入文件
在应用中添加 NLog.config,并将它复制到运行目录。
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File"
fileName="${basedir}/Logs/${shortdate}.log"
layout="${longdate} ${level} ${logger} ${message} ${exception:format=tostring}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" />
</rules>
</nlog>
SDK 风格项目可在 .csproj 中配置复制。
<ItemGroup>
<None Update="NLog.config" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
在应用启动时注册工厂,再通过 GetLogger 获取 logger。
using System;
using System.IO;
using LogInterface;
using LoggerNLog;
Logs.LogFactory = new NLogFactory(Path.Combine(AppContext.BaseDirectory, "NLog.config"));
ILogger logger = Logs.LogFactory.GetLogger("Worker");
logger.Info("应用启动");
logger.Info(new { OrderId = 42, Status = "Started" });
logger.Error(new InvalidOperationException("连接失败"), new { OrderId = 42 });
GetLogger(Type) 和 GetLogger<T>() 使用类型的简单名称。结构化对象消息以 JSON 写入 ${message}。序列化失败时会记录简短的诊断文本,传入的原始异常仍由 ${exception} 输出。AppID 和 InstanceID 可用 ${event-properties:item=AppID} 和 ${event-properties:item=InstanceID} 渲染。
全局配置与依赖注入
new NLogFactory() 和 new NLogFactory(appId, instanceId) 使用 NLog 的全局配置。应用可以通过运行目录的 NLog.config 或 NLog.LogManager.Configuration 提供这份配置。使用文件路径或配置对象构造的工厂拥有独立配置。
using LoggerNLog;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddNLog();
AddNLog() 将使用全局 NLog 配置的 NLogFactory 注册为 ILogFactory 单例。由应用通过构造函数注入 ILogFactory,随后调用 GetLogger。
程序化配置与生命周期
using LoggerNLog;
using NLog.Targets;
using var factory = NLogFactory.Create(config =>
{
var console = new ConsoleTarget("console") { Layout = "${level} ${message}" };
config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, console);
});
var logger = factory.GetLogger("Worker");
logger.Info("开始处理");
factory.Flush();
Create 构建与当前工厂绑定的 LoggingConfiguration。运行期间可修改 factory.Configuration 的目标或规则,再调用 factory.Reconfigure() 刷新已有 logger。传入预建的 LoggingConfiguration 时,也通过 Reconfigure() 应用后续改动。
独立工厂的 Flush() 刷新待写入日志,Dispose() 刷新并关闭其目标。替换 Logs.LogFactory 时会先释放旧的 IDisposable 工厂,替换后需要重新获取此前缓存的 logger。并行运行的独立工厂使用默认文件目标时,应写到不同文件。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- CSoft.LogInterface (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- NLog (>= 6.2.2)
- System.Text.Json (>= 10.0.5)
-
net10.0
- CSoft.LogInterface (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- NLog (>= 6.2.2)
-
net11.0
- CSoft.LogInterface (>= 11.0.0)
- NLog (>= 6.2.2)
-
net8.0
- CSoft.LogInterface (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- NLog (>= 6.2.2)
-
net9.0
- CSoft.LogInterface (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- NLog (>= 6.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 11.0.0 | 54 | 11/11/2026 | |
| 10.0.4 | 183 | 3/18/2026 | |
| 10.0.3 | 160 | 1/15/2026 | |
| 10.0.2 | 151 | 1/14/2026 | |
| 10.0.1 | 502 | 12/10/2025 | |
| 10.0.0 | 359 | 11/12/2025 | |
| 9.1.0 | 244 | 10/24/2025 | |
| 9.0.1 | 259 | 8/11/2025 | |
| 9.0.0 | 241 | 11/22/2024 | |
| 8.0.2 | 273 | 7/10/2024 | |
| 8.0.1 | 381 | 12/29/2023 | |
| 8.0.0 | 322 | 11/15/2023 | |
| 7.0.4 | 336 | 8/22/2023 | |
| 7.0.3 | 380 | 6/13/2023 | |
| 7.0.2 | 454 | 4/21/2023 | |
| 7.0.1 | 566 | 2/2/2023 | |
| 7.0.0 | 686 | 11/9/2022 | |
| 6.0.5 | 774 | 9/23/2022 | |
| 6.0.4 | 799 | 8/11/2022 | |
| 6.0.3 | 791 | 6/1/2022 |
新增 .NET 11 支持并更新 NLog 依赖