CSoft.LoggerNLog 11.0.0

dotnet add package CSoft.LoggerNLog --version 11.0.0
                    
NuGet\Install-Package CSoft.LoggerNLog -Version 11.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSoft.LoggerNLog" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSoft.LoggerNLog" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CSoft.LoggerNLog" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSoft.LoggerNLog --version 11.0.0
                    
#r "nuget: CSoft.LoggerNLog, 11.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CSoft.LoggerNLog@11.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSoft.LoggerNLog&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CSoft.LoggerNLog&version=11.0.0
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

新增 .NET 11 支持并更新 NLog 依赖