XYS.Utils.Log4Net 2.3.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package XYS.Utils.Log4Net --version 2.3.2
                    
NuGet\Install-Package XYS.Utils.Log4Net -Version 2.3.2
                    
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="XYS.Utils.Log4Net" Version="2.3.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XYS.Utils.Log4Net" Version="2.3.2" />
                    
Directory.Packages.props
<PackageReference Include="XYS.Utils.Log4Net" />
                    
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 XYS.Utils.Log4Net --version 2.3.2
                    
#r "nuget: XYS.Utils.Log4Net, 2.3.2"
                    
#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 XYS.Utils.Log4Net@2.3.2
                    
#: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=XYS.Utils.Log4Net&version=2.3.2
                    
Install as a Cake Addin
#tool nuget:?package=XYS.Utils.Log4Net&version=2.3.2
                    
Install as a Cake Tool

XYS.Utils.Log4Net

基于 log4net 的日志工具类库:按 前缀 + 级别 分离日志输出,支持自动捕获调用方法名、异常记录与格式化日志。

目标框架

net8.0 · net462 · netcoreapp3.1 · netstandard2.0 · netstandard2.1

依赖


配置

程序集在加载时通过特性自动读取 log4net 配置:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "xys.log4net.config.xml", Watch = true)]
  • 配置文件名固定为 xys.log4net.config.xml,需放在应用程序运行目录下(相对基目录解析)。
  • Watch = true:运行时修改配置会被自动重新加载。
  • 静默失败:若配置文件缺失或未定义 appender,log4net 不会报错,而是不产生任何日志输出。部署时请确认该文件已随程序一同发布。

logger 命名规则

每条日志按 {前缀}{级别后缀} 解析 logger,后缀为 -Debug / -Info / -Warn / -Error / -Fatal。例如前缀传 Order,Error 级别日志会写入名为 Order-Error 的 logger。可在配置中按此命名把不同来源、不同级别的日志分流到不同文件。


LogUtil

静态日志工具类,命名空间 XYS.Utils.Log4Net。提供 Debug / Info / Warn / Error / Fatal 五个级别,每级均含普通、含异常、格式化三类重载。

using XYS.Utils.Log4Net;

// 最简:仅传前缀与内容,方法名由 [CallerMemberName] 自动填充
LogUtil.Info("Order", "订单创建成功");

// 记录异常
try { /* ... */ }
catch (Exception ex)
{
    LogUtil.Error("Order", "订单创建失败", ex);
}

// 显式指定类型名 / 方法名
LogUtil.Warn("Order", nameof(OrderService), "Create", "库存不足");

// 格式化日志
LogUtil.InfoFormat("Order", nameof(OrderService), "Create", "订单 {0} 金额 {1}", orderId, amount);

// 格式化 + 异常
LogUtil.ErrorFormat("Order", nameof(OrderService), "Create", "订单 {0} 处理失败", ex, orderId);

日志正文格式为:类型名(左对齐32) 方法名(左对齐24)-内容,便于日志按列对齐阅读。

健壮性

日志记录不会因参数问题打断业务调用方:

  • typeName / methodNamenull 时按空串处理,不抛 NullReferenceException
  • *Format 系列在格式串与参数不匹配时捕获 FormatException,退化为输出原始格式串,不向上抛出。
  • 各级别在写入前先判断 IsXxxEnabled,未启用的级别直接短路,避免无谓的字符串拼接与格式化开销。

版本

当前版本 2.3.2

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 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  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 (26)

Showing the top 5 NuGet packages that depend on XYS.Utils.Log4Net:

Package Downloads
XYS.FR

Package Description

XYS.DataFactory

Package Description

XYS.Utils.DB

Package Description

XYS.Utils.Web

Package Description

XYS.Lab.Report

Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.5.0 254 7/24/2026
2.3.2 148 7/23/2026
2.3.1 353 7/14/2026
2.1.1.1 1,248 12/2/2023
2.1.0.3 1,898 11/23/2022
2.1.0.2 1,824 9/13/2022
2.1.0.1 7,462 1/23/2022
2.0.2.2 6,610 12/21/2021
2.0.2.1 1,673 12/20/2021
2.0.1.5 1,120 12/20/2021
2.0.1.4 13,122 5/31/2021
2.0.1.3 5,059 5/10/2021
2.0.1.2 900 5/10/2021
2.0.1.1 1,252 4/22/2021
2.0.0.1 1,341 2/20/2021
1.0.3.2 594 2/8/2021
1.0.3.1 582 2/8/2021
1.0.2.4 596 2/7/2021
1.0.2.3 603 2/7/2021
Loading failed