CodeWF.Log.Core 12.1.0.16

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

CodeWF.Log

NuGet NuGet License

面向 .NET 和 Avalonia 的轻量日志组件。当前版本为 12.1.0.16,核心目标是把“给用户看的内容”和“给维护人员排障的内容”明确分开。

用途 目标框架
CodeWF.Log.Core 文件日志、控制台输出和用户日志 Feed net8.0;net10.0
CodeWF.LogViewer.Avalonia LogView 和日志通知 net8.0;net10.0

版本统一维护在根目录 Directory.Build.props。运行 pack.bat 可将两个包输出到 artifacts/packages

初始化

每个进程必须在首次写日志或创建 LogView 前初始化一次:

Logger.Initialize(new LoggerOptions
{
    MinimumLevel = LogType.Info,
    EnableConsole = true,
    QueueCapacity = 10_000,
    RecentUserLogCapacity = 2_000,
    File = new FileLogOptions
    {
        DirectoryPath = Path.Combine(Environment.CurrentDirectory, "Log"),
        MaxFileSizeBytes = 100L * 1024 * 1024,
        BatchSize = 200,
        FlushInterval = TimeSpan.FromMilliseconds(500)
    }
});

DirectoryPath 是最终日志目录,组件不会再追加子目录。相对路径会在初始化时按当时的工作目录转换为绝对路径,因此多实例程序应由调用方按实例工作目录传入路径。

程序正常退出前调用:

await Logger.ShutdownAsync();

需要在运行中确保已落盘时调用 await Logger.FlushAsync();它不能替代退出时的 ShutdownAsync()

用户内容与技术内容

普通方法写文件,并把用户内容发送到已启用的控制台、LogView 和通知订阅方:

Logger.Info("任务已保存。");

Logger.Error(
    $"反序列化任务文件失败:{taskFilePath}",
    exception,
    "无法打开任务:任务文件内容不正确或与当前版本不兼容,请重新导出任务文件。");
  • 文件记录技术消息、用户提示和完整异常堆栈。
  • 控制台、LogView 和通知只接收 userMessage
  • 未传 userMessage 时,普通 message 同时作为用户内容显示。
  • 用户日志对象 UserLogEntry 不持有异常对象,展示层不会意外泄露堆栈。

只写文件时使用:

Logger.DebugToFile("连接参数:127.0.0.1:2700");
Logger.ErrorToFile("关闭后台队列失败。", exception);

可用方法包括 Debug/Info/Warn/Error/Fatal 和对应的 *ToFile 方法。Logger.MinimumLevel 是全局采集门槛,低于该级别的日志不会进入任何输出端。

Avalonia LogView

<Window
    xmlns="https://github.com/avaloniaui"
    xmlns:log="https://codewf.com">
    <Grid>
        <log:LogView
            MinimumLevel="Warn"
            MaximumLevel="Error"
            MaxDisplayCount="1000"
            RefreshInterval="00:00:00.100"
            TimestampFormat="yyyy-MM-dd HH:mm:ss.fff" />
    </Grid>
</Window>

MinimumLevelMaximumLevel 都是强类型 LogType 属性,可以绑定到 ViewModel 中的枚举属性:

public LogType MinimumLevel { get; set; } = LogType.Info;
public LogType MaximumLevel { get; set; } = LogType.Error;

每个 LogView 独立过滤和清空,互不影响。修改过滤范围时,控件会从最近用户日志缓存重新构建当前视图。

全局级别与视图级别的关系:

  • Logger.MinimumLevel 决定日志是否被采集,是全局门槛。
  • LogView.MinimumLevel/MaximumLevel 只决定当前控件显示哪些已采集日志。
  • 视图不能显示低于全局门槛、从未被采集的日志。

Avalonia 通知

通知独立于 LogView,在 App.axaml 中统一配置一次:

<Application
    xmlns="https://github.com/avaloniaui"
    xmlns:log="https://codewf.com"
    log:LogNotifications.Mode="DesktopWindow"
    log:LogNotifications.MinimumLevel="Error"
    log:LogNotifications.MaximumLevel="Fatal"
    log:LogNotifications.Duration="00:00:10"
    log:LogNotifications.ApplicationName="设备服务客户端">
</Application>

Mode 支持:

  • None:关闭通知。
  • InApp:在当前活动窗口内显示通知。
  • DesktopWindow:显示桌面通知窗口。

通知只接收启用后的新用户日志,不回放历史,也不会接收 *ToFile 日志。最小/最大级别同样是强类型 LogType 属性。组件在显示时动态选择活动窗口或主窗口,多窗口程序仍只创建一个通知订阅。桌面通知是主窗口的 owned window,主窗口关闭时会立即随之关闭,不会延迟应用退出。桌面窗口继续支持自定义正文模板、显示时间和 DesktopNotificationAttentionMode

Demo

  • ConsoleLogDemo:演示用户/技术内容分流、异常堆栈、文件专用日志、并发写入、Flush 和文件轮转。
  • AvaloniaLogDemo:用操作按钮和三个日志视图演示友好异常、文件专用日志、并发写入以及独立级别过滤;通知在 App.axaml 中统一配置,默认无需注册主题资源,并可动态切换一组自定义资源覆盖。

12.1 升级提示

这是一次不保留旧语义的主版本升级。需要删除旧的 RecordToFileLogDirEnableConsoleOutputlog2UI/log2File/log2ConsoleLogView.Notification* 用法,统一改为:

  1. 启动时调用 Logger.Initialize(LoggerOptions)
  2. 技术异常使用 Logger.Error(technicalMessage, exception, userMessage)
  3. 内部诊断使用 *ToFile
  4. 通知在 App.axaml 中使用应用级 LogNotifications 附加属性。
  5. 退出时调用 Logger.ShutdownAsync()
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on CodeWF.Log.Core:

Package Downloads
CodeWF.LogViewer.Avalonia

面向 Avalonia 的 C# 日志查看控件,基于 CodeWF.Log.Core 展示实时日志、级别信息和界面友好内容,适合桌面应用内嵌日志观察台。

CodeWF.NetWrapper

CodeWF.NetWrapper builds on CodeWF.NetWeaver and provides TCP/UDP helpers and command dispatching for socket applications.

CodeWF.EventBus.Socket

基于 Socket 实现的分布式事件总线,不依赖第三方 MQ。

CodeWF.Log.Avalonia

面向 Avalonia 的 C# 日志查看控件,基于 CodeWF.Log.Core 展示实时日志、级别信息和界面友好内容,适合桌面应用内嵌日志观察台。

CodeWF.Log.Extensions.Logging

Microsoft.Extensions.Logging provider for CodeWF.Log.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
12.1.1.1 50 7/30/2026
12.1.0.28 76 7/29/2026
12.1.0.24 107 7/27/2026
12.1.0.21 105 7/27/2026
12.1.0.19 132 7/27/2026
12.1.0.18 122 7/24/2026
12.1.0.17 163 7/23/2026
12.1.0.16 174 7/23/2026
12.1.0.7 114 7/22/2026
12.1.0.4 112 7/16/2026
12.1.0.3 114 7/16/2026
12.1.0.1 180 7/14/2026
12.0.5.5 147 6/25/2026
12.0.5.4 127 6/25/2026
12.0.5.3 137 6/24/2026
11.3.14.11 70 7/29/2026
11.3.14.7 99 7/27/2026
11.3.14.4 104 7/27/2026
11.3.14.2 110 7/24/2026
11.3.14.1 145 7/22/2026
Loading failed