XZJ.Logger
1.1.5
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Framework 4.6.2
This package targets .NET Framework 4.6.2. The package is compatible with this framework or higher.
dotnet add package XZJ.Logger --version 1.1.5
NuGet\Install-Package XZJ.Logger -Version 1.1.5
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="XZJ.Logger" Version="1.1.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XZJ.Logger" Version="1.1.5" />
<PackageReference Include="XZJ.Logger" />
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 XZJ.Logger --version 1.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: XZJ.Logger, 1.1.5"
#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 XZJ.Logger@1.1.5
#: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=XZJ.Logger&version=1.1.5
#tool nuget:?package=XZJ.Logger&version=1.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
这是一个简单的日志记录库
- XZJ.Logger 是一个用于记录日志信息的简单库,支持多种日志级别和输出方式。
- 该库可以帮助开发者轻松地在应用程序中添加日志功能,以便于调试和监控应用程序的运行状态。
- 主打一个简单易用。
- 日志输出是通过扩展 Object 类实现的,可以直接在任何对象上调用日志方法。
- 问题反馈邮箱 330532631@qq.com
示例
将日志输出到控制台的示例代码
this.Logger().Debug("Hello, World!").Console();
this.Logger().Info("Hello, World!").Console();
this.Logger().Error("Hello, World!").Console();
this.Logger().Operation("Hello, World!").Console();
this.Logger().Warning("Hello, World!").Console();
将日志通过事件触发的示例代码
- 将日志以事件触发的方式输出,可以使用 WeakReferenceMessenger 来注册和接收日志事件。
- WPF 等图形程序 可以使用此方式将日志显示在界面上。
//将孤舟衰笠翁输出,并触发事件,UILogEventMsg
this.Logger().Debug("终不似,少年游").Event("柳宗元
//订阅事件
LogOperation.LogEvent += m =>
{
//MsgFenLeiCiTiao 消息分类词条
if (m.MsgFenLeiCiTiao == "枊宗元")
{
Console.WriteLine("只接收事件日志为 枊宗元的日志" + m.Message);
}
else
{
Console.WriteLine("接收除了 枊宗元 的其它日志" + m.Message);
}
};
将日志输出到文件的示例代码
- 将日志写入文本文件,可以指定文件名。如果不指定文件名,默认使用日志级别作为文件名。
//将千山鸟飞绝写入文本文件,文件名为Info
this.Logger().Info("千山鸟飞绝").File();
//将万径人踪灭写入文本文件,文件名为 千万孤独
this.Logger().Info("万径人踪灭").File("千万孤独");
- 文本文件日志的默认配置
/// <summary>
/// 文本文件日志配置
/// </summary>
public class LogConfig
{
/// <summary>
/// 日志文件保存基路径 默认路径在应用程序目录下的Logs文件夹
/// </summary>
public string BasePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs\\");
/// <summary>
/// 日志文件创建周期 默认按天创建,如果创建日志类型为 Always 默认只会使用这一个文件,且不限大小,不会分文件
/// </summary>
public CreatePeriod CreatePeriod { get; set; } = CreatePeriod.Day;
/// <summary>
/// 日志保存天数 默认30天,每24小时检查一次
/// </summary>
public int SaveDay { get; set; } = 30;
/// <summary>
/// 单个日志文件最大大小(MB) 默认100MB,设为0则不限制大小
/// </summary>
public long MaxFileSizeMB { get; set; } = 100;
/// <summary>
/// 日志文件编码 默认UTF-8(带BOM,解决记事本中文乱码)
/// </summary>
public Encoding FileEncoding { get; set; } = new UTF8Encoding(true);
/// <summary>
/// 日志格式 默认:[时间] [级别] [线程ID] 消息
/// 可用占位符:{CreateDateTime}、{Level}、{ThreadId}、{Message}
/// </summary>
public ILogFormat LogFormat { get; set; } = new DefaultForamt();
/// <summary>
/// 是否记录调用上下文(类名、方法名、行号),默认启用
/// 生产环境可关闭以提升性能
/// </summary>
public bool EnableCallerInfo { get; set; } = true;
}
- 可以通过执行 LogOperation 来更改日志文件的保存路径、创建周期和保存天数。
//修改文本文件日志配置 可动态修改
LogOperation.SetLogConfig(new TextFileLogConfig()
{
BasePath = @"C:\Logs\\",
CreatePeriod = CreatePeriod.Day,
SaveDay = 7,
//其它属性配置
});
混合输出日志的示例代码
//将日志 "夕阳山外山" 同时输出到控制台、事件、和文本文件.文件名为 "夕阳无限好"
this.Logger().Warning("夕阳山外山").Console().Event().File("夕阳无限好");
安装
- 可以通过 NuGet 包管理器安装 XZJ.Logger 库。
- 开箱即用,无需额外配置。
- 当然,如果要指定文本文件日志的保存路径、创建周期和保存天数,可以通过 LogOperation.TextFileLoggerConfig 方法进行配置。
更新 1.1.1
静态类里面无法使用扩展方法的问题,已修复。
public static class TestExtension
{
public static void TestMethod()
{
LoggerEx.Logger().Info("这是一个测试扩展方法的日志");
}
}
更新 1.1.2
- 新增 后缀 OnlyDebug 方法,仅在 DEBUG 模式下输出日志,RELEASE 模式下不输出日志。
方法演示
//仅在 DEBUG 模式下输出日志 RELEASE模式下不输出
this.Logger().DebugOnlyDebug("This is a debug message.").Console();
this.Logger().WarningOnlyDebug("This is a debug message.").Console();
this.Logger().InfoOnlyDebug("This is a debug message.").Console();
this.Logger().OperationOnlyDebug("This is a debug message.").Console();
this.Logger().ErrorOnlyDebug("This is a debug message.").Console();
方法原型
/// <summary>
/// 输出调试日志, 仅 DEBUG 模式下输出日志,RELEASE模式下不输出
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
public LogOperation DebugOnly(string log)
{
#if DEBUG
LogContext logContext = new LogContext() { Message = log, Level = Level.DEBUG, CreateDateTime = GetDateTime() };
return new LogOperation(logContext);
#endif
}
更新 1.1.3
修复部分BUG
去掉MVVM 强依赖 事件日志订阅改为
//订阅事件
LogOperation.LogEvent += m =>
{
//MsgFenLeiCiTiao 消息分类词条
if (m.MsgFenLeiCiTiao == "枊宗元")
{
Console.WriteLine("只接收事件日志为 枊宗元的日志" + m.Message);
}
else
{
Console.WriteLine("接收除了 枊宗元 的其它日志" + m.Message);
}
};
日志大小限制
//默认 100 MB 超过大小会自动生成带序号的新文件 ,序号最大不能超过 999 ,超过了则不会再写。
public long MaxFileSizeMB { get; set; } = 100;
新增日志格式化
日志自定义格式化
通过接口 ILogFormat 接口来实现 以下是默认格式,仅供参考。如果不需要自定义格,就什么也不用管。
/// <summary>
/// 默认格式
/// </summary>
public class DefaultForamt : ILogFormat
{
/// <summary>
/// 默认格式
/// </summary>
/// <param name="logContext"></param>
/// <returns></returns>
public string Foramt(LogContext logContext)
{
string log = "[ " + logContext.CreateDateTime + " ] " + logContext.Level + " " + logContext.Message + "\r\n";
return log;
}
}
实现自定义格式 最后还需要调用一下方法 LogOperation.SetLogFormat(new CustomForamt());
/// <summary>
/// 自定义格式
/// </summary>
public class CustomFormat : ILogFormat
{
//每行日志都会带有 恭喜发财 的字样
public string Format(LogContext logContext)
{
//创建时间
string log =logContext.CreateDateTime+" "+
//输出执行 类名、方法名、行号
logContext.CallerInfo.ToString()+
//输出日志级别
logContext.Level +
//输出线程ID
logContext.ThreadId+
//输出自定义文字
" 恭喜发财 " +
//输出日志消息 加换行
logContext.Message + "\r\n";
return log;
}
}
调用方法设置格式
public void TestMethod()
{
LogOperation.SetLogForamt(new CustomForamt());
}
全局异常捕获
- 全局异常处理(适配控制台/WinForm/WPF) 不支持格式化输出
- -全局异常捕获的作用是 “记录异常”,不是 “让代码跳过异常继续执行”;
//初始化全局异常捕获(程序入口调用一次即可)
GlobalExceptionHandler.Init();
更新 1.1.4
- 加入MIT 许可证
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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 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. |
| .NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.6.2
- No dependencies.
-
net10.0
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on XZJ.Logger:
| Package | Downloads |
|---|---|
|
XZJ.Communication
一个简单的通信库,基于NModbus 的扩展开发,短报文 TCPServer,TCPClient |
|
|
XZJ.PropertyInstantStorage
一个简单的属性数据即时存储库 |
GitHub repositories
This package is not used by any popular GitHub repositories.