MasterServer.Lib.Logging.NLog
2.0.0
dotnet add package MasterServer.Lib.Logging.NLog --version 2.0.0
NuGet\Install-Package MasterServer.Lib.Logging.NLog -Version 2.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="MasterServer.Lib.Logging.NLog" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MasterServer.Lib.Logging.NLog" Version="2.0.0" />
<PackageReference Include="MasterServer.Lib.Logging.NLog" />
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 MasterServer.Lib.Logging.NLog --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MasterServer.Lib.Logging.NLog, 2.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 MasterServer.Lib.Logging.NLog@2.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=MasterServer.Lib.Logging.NLog&version=2.0.0
#tool nuget:?package=MasterServer.Lib.Logging.NLog&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MasterServer.Lib.Logging.NLog
基于NLog的日志实现库,提供配置文件和代码配置支持。 作者邮箱:dengjianhua1999@qq.com
特性
- 基于NLog 6.x的强大日志功能
- 支持配置文件(nlog.config)
- 支持代码配置(默认控制台+文件)
- 集成Microsoft.Extensions.Logging
- 支持多种日志目标(文件、控制台、数据库等)
- 支持日志级别过滤
- 异步日志记录 - 日志写入不阻塞主线程
- 自动目录创建 - 日志目录不存在时自动创建
- 优雅关闭 - 提供Shutdown方法确保日志完整写入
使用方式
1. 在依赖注入中使用
using Microsoft.Extensions.DependencyInjection;
using MasterServer.Lib.Logging;
var services = new ServiceCollection();
// 添加NLog日志支持,使用配置文件
services.AddNlog("nlog.config");
// 或使用默认配置
services.AddNlog();
var serviceProvider = services.BuildServiceProvider();
// 使用日志
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
logger.Info("应用程序启动");
2. 在依赖注入中使用(配合MasterServer.Lib.Ioc项目,推荐)
using Microsoft.Extensions.DependencyInjection;
using MasterServer.Lib.Logging;
using MasterServer.Lib.Ioc;
var services = new ServiceCollection();
services.AddNlog();
// 注册服务
ServiceInstaller.RegisterToIocFromAssembly(services, "Test");
var logger = ServiceInstaller.Resolve<ILogger<PlcService>>();
logger.Info("测试3");
3. 静态初始化(无DI,推荐)
using MasterServer.Lib.Logging;
// 初始化NLog。三个参数都有默认值,可以为空
NlogExtensions.InitializeNLog("nlog.config", "logs", NLog.LogLevel.Info);
// 使用静态日志
LoggerExtensions.Info("这是一条信息日志");
LoggerExtensions.Error("这是一条错误日志");
4. 应用程序关闭时确保日志完整写入
// 在应用程序退出前调用,确保异步日志被写出
NlogExtensions.ShutdownNLog();
重要提示: 如果使用异步日志记录(默认配置),务必在应用程序关闭时调用ShutdownNLog(),否则可能丢失最后几条日志。
3. NLog配置文件示例 (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">
<targets>
<target name="logfile" xsi:type="File" fileName="logs/log-${shortdate}.log"
layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
<target name="logconsole" xsi:type="Console"
layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
安装
通过NuGet安装:
Install-Package MasterServer.Lib.Logging.NLog
依赖
- NLog
- NLog.Extensions.Logging
- Microsoft.Extensions.Logging
- MasterServer.Lib.Logging</content>
| 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 was computed. 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 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.
-
.NETStandard 2.0
- MasterServer.Lib.Logging (>= 2.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.7)
- NLog.Extensions.Logging (>= 6.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Nlog日志包