CodeWF.Log.Core 11.3.14.7

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

CodeWF.Log

NuGet NuGet NuGet License

CodeWF.Log 是面向 .NET 和 Avalonia 的轻量日志组件,提供文件/控制台输出、显式申请的重要日志通知,以及可选的 LogView。新版本以 Microsoft.Extensions.Logging 为主入口,同时保留 Logger.Info/Warn/Error/FatalLogger.*ToFile 等静态 API;不需要界面日志的应用无需放置 LogView

完整设计约束见 CodeWF.Log 设计

Packages

Package Purpose Targets
CodeWF.Log.Core 文件/控制台日志、完整事件 Feed、静态 Logger API net10.0
CodeWF.Log.Extensions.Logging Microsoft.Extensions.Logging Provider 和 AddCodeWF() net10.0
CodeWF.Log.Avalonia 可独立使用的日志通知,以及可选的 LogView net10.0

运行 pack.bat 可将三个包输出到 artifacts/packages

五种典型使用示例

# 场景 需要的包/能力 Demo
1 Console + File 只引用 CodeWF.Log.Core;关闭 EventFeed,不包含 Avalonia、LogView 或通知 ConsoleDemo
2 多 Avalonia LogView + File + 通知 三个分级 LogView、文件输出、模板切换和显式通知,使用 MEL/DI LogViewDemo
3 无 Avalonia LogView + File + 通知 保持 EventFeed 开启供通知订阅,但窗口中不放置 LogView FileNotifyDemo
4 配合 Serilog 使用 Serilog 负责文件/控制台,CodeWF 只启用 EventFeed、LogView 和通知 SerilogDemo
5 Web API CodeWF.Log.Core + CodeWF.Log.Extensions.Logging,通过 AddCodeWF() 接入 MEL WebApiDemo

ConsoleDemo 是最小依赖场景:日志组件只做核心 Console/File Pipeline,不会加载 Avalonia,也没有产生系统弹窗的通道。

Microsoft.Extensions.Logging

推荐新项目使用标准 .NET 日志入口:

builder.Logging.AddCodeWF();

默认约定:

  • MEL 自身负责全局级别、Category 级别和多 Provider 编排。
  • CodeWF 默认写入 AppContext.BaseDirectory/logs
  • 普通 ILoggerLogUser* 都生成完整 CodeWFLogEvent,进入启用的 File、Console 和 LogEventFeed
  • LogUser* 只额外提供 UserMessage;模板中的 {UserMessage} 为空白时回退 {Message}
  • File 使用独立 OutputTemplate;Console、可选的 LogView 和通知管线共享 LineTemplate,默认组合式 DesktopWindow 避免重复显示级别和时间。
  • 两类模板都可通过各自的 Controller 显式、原子地运行时更新;其他 Pipeline 配置仍需重启生效。

常用配置使用结构化 Options;日志格式由 OutputTemplate 决定,不提供 IncludeEventIdIncludeScopes 这类开关。模板里写了对应占位符就输出,没有写就忽略:

builder.Logging.AddCodeWF(options =>
{
    options.File.Enabled = true;
    options.File.DirectoryPath = "Log";
    options.File.OutputTemplate =
        "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] ({Category}) {Message} {Properties}{NewLine}{Exception}";

    options.LineTemplate =
        "{Timestamp:HH:mm:ss} [{Level:u3}] ({Category}) {UserMessage}{NewLine}";

    options.Console.Enabled = false;
    options.Capture.Scopes = true;
    options.Capture.Activity = true;
    options.Queue.Capacity = 10_000;
});

常用模板占位符:TimestampLevelCategoryEventIdEventNameMessageMessageTemplateUserMessagePropertiesScopesActivityTraceIdSpanIdExceptionNewLine

对应的 appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },
    "CodeWF": {
      "LogLevel": {
        "Default": "Trace",
        "Microsoft.AspNetCore": "Warning"
      },
      "File": {
        "Enabled": true,
        "DirectoryPath": "Log",
        "OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] ({Category}) {Message} {Properties}{NewLine}{Exception}"
      },
      "Console": {
        "Enabled": false
      },
      "LineTemplate": "{Timestamp:HH:mm:ss} [{Level:u3}] ({Category}) {UserMessage}{NewLine}",
      "EventFeed": {
        "Enabled": true,
        "RecentCapacity": 2000
      }
    }
  }
}

如果希望控制台统一使用 CodeWF 的 LineTemplate,应避免同时注册其他 Console Provider:

builder.Logging.ClearProviders();
builder.Logging.AddCodeWF(options =>
{
    options.Console.Enabled = true;
    options.LineTemplate =
        "{Timestamp:HH:mm:ss} [{Level:u3}] ({Category}) {UserMessage}{NewLine}";
});
logger.LogError(ex, "Failed to parse task file {TaskPath}", taskPath);

logger.LogUserError(
    ex,
    "任务文件加载失败,请检查文件格式后重新打开。",
    "Failed to parse task file {TaskPath}",
    taskPath);

Legacy Static API

非 Host 场景仍可使用 Logger.Initialize(...)

Logger.Initialize(new LoggerOptions
{
    MinimumLevel = LogLevel.Debug,
    EnableConsole = true,
    File = new FileLogOptions
    {
        DirectoryPath = Path.Combine(AppContext.BaseDirectory, "Log")
    }
});

静态 API 约定:

  • Logger.Info/Warn/Error/Fatal(...) 写入完整事件;userMessage 是同一事件上的可选字段,requestNotification 默认 false
  • Logger.Error(message, exception, userMessage) 分别保存诊断消息、异常快照和用户消息。
  • 只有显式传入 requestNotification: true 且日志级别达到 LogNotifications.MinimumLevel 时,日志才有资格触发通知。
  • Logger.*ToFile(...) 只写文件,不进入 Console、LogView 或通知。
  • Logger.MinimumLevelLoggerOptions.MinimumLevel 使用 MEL 标准 LogLevel

退出前调用:

await Logger.ShutdownAsync();

运行时切换格式时,DI 场景注入 ILineTemplateControllerIFileOutputTemplateController;静态 API 场景使用 Logger.Events.LineTemplateLogger.FileOutputTemplate。模板校验失败时当前有效格式保持不变。

Avalonia

v11.3.14 分支支持 Avalonia [11.3.14, 12.0.0),组件包版本为 11.3.14.x

日志通知和 LogView 是两项独立能力。只需要“日志文件 + 重要日志桌面通知”的应用仍需引用 CodeWF.Log.Extensions.LoggingCodeWF.Log.Avalonia,但窗口中不需要 <log:LogView />

文件日志 + 重要通知(无 LogView)

注册 Provider 时开启文件和事件 Feed。通知通过事件 Feed 接收新日志,所以不能把 EventFeed.Enabled 关闭:

var logDirectory = Path.Combine(AppContext.BaseDirectory, "Log");

services.AddLogging(builder =>
{
    builder.SetMinimumLevel(LogLevel.Trace);
    builder.AddCodeWF(options =>
    {
        options.File.Enabled = true;
        options.File.DirectoryPath = logDirectory;
        options.Console.Enabled = false;
        options.EventFeed.Enabled = true;
    });
});

App.axaml 上配置通知。MinimumLevel 是级别门槛,不会让普通 Error 日志自动弹窗:

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

MEL/DI 场景还要把当前 Provider 的事件 Feed 交给通知组件:

public override void OnFrameworkInitializationCompleted()
{
    LogContext.SetSource(this, Program.Services.GetRequiredService<LogEventFeed>());
    base.OnFrameworkInitializationCompleted();
}

普通日志只写入启用的输出;重要日志使用 LogUserNotification(...) 显式申请通知:

logger.LogError("设备服务首次连接失败,后台继续重试。"); // 记录日志,不弹窗

logger.LogUserNotification(
    LogLevel.Error,
    "设备服务连接已中断,请检查服务状态。",
    "Device service connection failed after {RetryCount} retries",
    retryCount);

最终通知条件是:Mode != None && Level >= MinimumLevel && RequestNotification。普通 LogError(...)LogUserError(...)Logger.Error(...) 默认都不弹窗;静态 API 只有显式传入 requestNotification: true 才申请通知。Logger.*ToFile(...) 不进入事件 Feed,因此永远不会触发通知。

DesktopWindow 默认使用 360px 宽的组合式重要日志窗口:Error 与 Critical 分别使用圆形/三角形图标和不同红色渐变;单条、多条、长内容对应 284/320/420px 可见高度。窗口复用期间追加的日志在标题栏显示“N条新日志”,多条日志可前后翻页,长内容在正文区域滚动。默认正文显示 UserMessage,为空时回退 Message,避免与窗口已经独立显示的级别和时间重复;完整 LineTemplate 格式化结果仍通过 LogNotificationContent.Content 提供给 DesktopContentTemplate,InApp 通知也继续使用该结果。

颜色、尺寸、按钮和渐变可以通过 LogNotificationResourceKeys 对应的动态资源覆盖;需要完全不同的内容布局时使用 LogNotifications.DesktopContentTemplate。组件内嵌默认图标,调用方不需要复制图片文件。

完整可运行示例见 FileNotifyDemo,其中没有任何 LogView,并提供 Error、Critical、长内容、连续追加、仅写日志和低于阈值等对比按钮。

可选的 LogView

需要在应用界面查看实时日志时,再放置 LogView。XAML 命名空间保持不变:

<Window
    xmlns="https://github.com/avaloniaui"
    xmlns:log="https://codewf.com">
    <log:LogView
        MinimumLevel="Information"
        MaximumLevel="Critical"
        MaxDisplayCount="1000" />
</Window>

LogView.MinimumLevelLogView.MaximumLevel 都是 Microsoft.Extensions.Logging.LogLevel,每个视图可以独立按区间显示完整事件。默认范围为 Information 至 Critical。

右键“查看日志”使用应用级 LogContext.LogDirectory,也可由单个 LogView.LogDirectory 覆盖。该路径与事件 Source 独立,适合 CodeWF 只负责界面、Serilog 负责文件的多 Provider 场景:

LogContext.SetLogDirectory(this, Path.GetFullPath("Log"));

通知模式

通知也可以和 LogView 同时使用,配置方式不变:

<Application
    xmlns="https://github.com/avaloniaui"
    xmlns:log="https://codewf.com"
    log:LogNotifications.Mode="DesktopWindow"
    log:LogNotifications.MinimumLevel="Error"
    log:LogNotifications.Duration="00:00:10"
    log:LogNotifications.ApplicationName="CodeWF Log Demo" />

LogNotifications 只接收 RequestNotification=true 且达到 MinimumLevel 的新事件,不回放历史,也不会接收 *ToFile 日志。静态 Logger 通过 requestNotification: true 显式请求;ILogger<T> 通过 LogUserNotification(...) 请求。InApp 默认最多同时显示 3 条;DesktopWindow 复用一个桌面右下角窗口,并在同一窗口显示新日志计数和翻页状态。通知展示队列溢出时提示查看日志文件,不假定应用存在 LogView

Logger.Error(
    "设备服务心跳超时。",
    userMessage: "设备服务连接已中断,请检查服务状态。",
    requestNotification: true);

logger.LogUserNotification(
    LogLevel.Error,
    "设备服务连接已中断,请检查服务状态。",
    "Device service heartbeat timed out for {TaskName}",
    taskName);

Demos

Demo Purpose
ConsoleDemo CodeWF.Log.Core:关闭 EventFeed,只演示静态 Logger、控制台、文件、*ToFile 和文件轮转;没有视图或弹窗。
LogViewDemo Avalonia 多 LogView:MEL/DI、分级视图、两类模板、结构化上下文、异常和通知。
FileNotifyDemo Avalonia 无 LogView:文件输出、文件模板切换,以及 Error/Critical、长内容、连续追加和通知门槛对比。
SerilogDemo Avalonia 联合 Provider:Serilog 负责文件/控制台,CodeWF 负责 LogView 和通知。
WebApiDemo ASP.NET Core:AddCodeWF()、配置绑定、Scope、Activity、LoggerMessage 和最近事件接口。
Product Compatible and additional computed target framework versions.
.NET 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.

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 46 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 113 7/16/2026
12.1.0.1 180 7/14/2026
12.0.5.5 146 6/25/2026
12.0.5.4 126 6/25/2026
12.0.5.3 136 6/24/2026
11.3.14.11 70 7/29/2026
11.3.14.7 99 7/27/2026
11.3.14.4 101 7/27/2026
11.3.14.2 110 7/24/2026
11.3.14.1 145 7/22/2026
Loading failed