Dijing.SerilogExt 2.0.0

dotnet add package Dijing.SerilogExt --version 2.0.0
                    
NuGet\Install-Package Dijing.SerilogExt -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="Dijing.SerilogExt" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dijing.SerilogExt" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Dijing.SerilogExt" />
                    
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 Dijing.SerilogExt --version 2.0.0
                    
#r "nuget: Dijing.SerilogExt, 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 Dijing.SerilogExt@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=Dijing.SerilogExt&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Dijing.SerilogExt&version=2.0.0
                    
Install as a Cake Tool

Dijing.SerilogExt

基于 Serilog 的日志配置封装,一行代码完成控制台、文件、Seq 与内存四路输出,并提供日志事件用于重定向到 UI。

安装

Install-Package Dijing.SerilogExt

或

dotnet add package Dijing.SerilogExt

目标框架 netstandard2.0,可直接用于 .NET Framework 4.6.1+ 与 .NET 6/8/9/10。

快速开始

using Dijing.SerilogExt;
using Serilog;

// 生产环境预设:输出 Information、文件 Warning、框架日志压到 Warning
InitLog.SetLog(LogOptions.Production());

Log.Information("程序启动 {Time}", DateTime.Now);
Log.Warning("磁盘剩余空间 {Free} MB", 1024);

// 退出前刷新缓冲
Log.CloseAndFlush();

初始化后直接使用 Serilog 的静态 Log 类写日志,不需要再自己构造 LoggerConfiguration。

级别模型

三个级别互相独立,靠约束绑定:

配置项 作用通道 默认值 约束
OutputLevel 控制台 + 内存 sink(UI) Information 全局门,最低
FileLevel 文件 Warning 不得低于 OutputLevel
FrameworkLevel Microsoft / System 命名空间 Warning 不得低于 OutputLevel;设为 null 表示不压制
SeqLevel Seq Warning 独立,可低于 OutputLevel

因此可以做到「控制台看全量、文件只留关键信息」:

using Serilog.Events;

var options = LogOptions.Production();
options.OutputLevel = LogEventLevel.Debug;        // 控制台与 UI 收到 Debug 及以上
options.FileLevel = LogEventLevel.Information;    // 文件只留 Information 及以上

InitLog.SetLog(options);

越界组合会在初始化时直接抛 ArgumentException,不做静默兜底:

options.OutputLevel = LogEventLevel.Warning;
options.FileLevel = LogEventLevel.Information;    // 低于输出级别
InitLog.SetLog(options);                          // ArgumentException

FrameworkLevel 之所以也要校验:Serilog 的 MinimumLevel.Override 是「替换」该命名空间的有效级别,取值低于输出级别时会突破输出下限,导致压制形同虚设。

预设

// 生产:输出 Information、文件 Warning、框架日志压到 Warning、Seq Warning
InitLog.SetLog(LogOptions.Production());

// 开发:输出 Debug、文件 Information、不压制框架日志、Seq Information
InitLog.SetLog(LogOptions.Development());

预设只是属性赋值的工厂方法,需要微调时直接改属性即可。

输出行为

SetLog 初始化后会挂载以下 sink:

Sink 接收级别 说明
Console OutputLevel 及以上 无控制台的进程(如 WinExe)输出会被系统丢弃,不抛异常
File FileLevel 及以上 按天滚动,默认 logs/log-.txt,单文件上限 10 MB,默认保留 30 个
Seq SeqLevel 及以上 仅 SeqUrl 非空时挂载
InMemorySink OutputLevel 及以上 通过事件供上层订阅,用于把日志推到界面

API

// 控制台模板带毫秒
InitLog.SetLog(LogOptions options);

// 精简模板(时间只到秒)
InitLog.SetLogSimple(LogOptions options);

// 模板不含日期前缀
InitLog.SetLogWithoutDate(LogOptions options);

三个方法只决定输出格式与内存 sink 类型,级别与路径全部由 LogOptions 控制:

var options = new LogOptions
{
    Path = "logs",
    FileSizeLimitBytes = 10485760,
    RetainedFileCountLimit = 30,
    OutputLevel = LogEventLevel.Information,
    FileLevel = LogEventLevel.Warning,
    FrameworkLevel = LogEventLevel.Warning,
    SeqUrl = "http://localhost:5341",
    SeqLevel = LogEventLevel.Warning,
    ApplicationName = "MyApp",
    MachineName = "SRV-01"
};

此外,SetLog 会自动接管 AppDomain.UnhandledException 与 ProcessExit,未捕获异常与进程退出都会留下一条 Warning 记录。该事件只在首次初始化时挂载一次(InitFlag 守卫);重复初始化会先 Log.CloseAndFlush(),把旧日志落盘并释放文件句柄,因此运行期切换日志目录或级别是安全的。

把日志接到界面

InMemorySink.OnLogReceivedEvent += (sender, message) =>
{
    // message 已按模板格式化,可直接追加到 TextBox / ListBox
    Dispatcher.BeginInvoke(new Action(() => logBox.AppendText(message + Environment.NewLine)));
};

事件在写日志的线程上触发,UI 场景请自行切回主线程。InMemorySimpleSink 提供同样的事件,模板更短(时间只到秒)。

从 1.x 迁移

2.0.0 移除了 RunModeEnum,改为 LogOptions:

1.x 2.0
SetLog(RunModeEnum.Debug) SetLog(LogOptions.Development())
SetLog(RunModeEnum.Release) SetLog(LogOptions.Production())
SetLog(RunModeEnum.Release, path: "x") SetLog(new LogOptions { Path = "x" })
SetLog(mode, appName, machineName, seqUrl) LogOptions 上设 ApplicationName / MachineName / SeqUrl
SetLog(mode, ..., fileLevel: X) LogOptions.FileLevel = X
带 Seq 的独立重载 合并进 LogOptions,方法由 4 个减为 3 个

两个行为变化需要注意:

  • 旧 Debug 模式会向 Seq 推送全量,新版本 SeqLevel 默认 Warning,需要全量请显式设为 Verbose 或 Information。
  • 旧 Debug 模式不压制框架日志,新版本 FrameworkLevel 默认 Warning,需要放开请设为 null。

.NET Framework 使用说明

本包为 netstandard2.0,net48 可直接引用,但 Serilog 各组件引用的程序集版本不同,必须启用绑定重定向,否则运行时报 FileLoadException 0x80131040。

在 .NET Framework 项目的 csproj 中确认存在:

<TargetFramework>net48</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

并且不要手写 App.config 里的 Serilog 重定向条目——交由 MSBuild 自动生成,随依赖版本变化而更新。

老式(非 SDK 风格 / packages.config)工程无法通过 ProjectReference 传递依赖,请改用 PackageReference 安装本包。

许可

本软件为专有软件,著作权归 Dijing 所有。未经著作权人书面许可,不得复制、传播、修改或将本软件用于商业目的。

完整条款见包内 LICENSE.txt;商业授权请联系 http://www.hgnet.tech 。

版本记录

2.0.0

  • 破坏性变更:移除 RunModeEnum,SetLog / SetLogSimple / SetLogWithoutDate 改为接收 LogOptions
  • 输出级别与文件记录级别分离,FileLevel 不得低于 OutputLevel,越界初始化即抛错
  • 新增 FrameworkLevel(可空,null 表示不压制),并校验不得低于输出级别
  • Seq 级别独立为 SeqLevel;带 Seq 的重载合并进 LogOptions,公开方法由 4 个减为 3 个
  • 新增 LogOptions.Production() / LogOptions.Development() 预设
  • 重复初始化先 Log.CloseAndFlush(),落盘缓冲并释放旧文件句柄
  • AppDomain 事件挂载由 InitFlag 守卫,避免重复订阅

1.3.0

  • 初始化方法新增可选参数 fileLevel,默认 Warning
  • 文件级别低于 Information 时全局最小级别同步下调
  • 重复初始化改为先 Log.CloseAndFlush()
  • AppDomain 事件挂载加 InitFlag 守卫

1.2.9

  • Serilog 升级至 4.4.0,程序集版本由 4.3.0.0 变为 4.4.0.0
  • Serilog.Sinks.Seq 升级至 9.1.0
  • net48 项目升级后须重新生成绑定重定向

1.2.8 及更早

  • 基础日志配置能力
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 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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Dijing.SerilogExt:

Package Downloads
Dijing.Proxy

proxy based on ssh.net

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 78 11/11/2026
1.2.9 82 11/10/2026
1.2.8 450 6/17/2025
1.2.7 342 5/26/2025
1.2.6 307 5/26/2025
1.2.5 293 5/19/2025
1.2.4 811 7/23/2024
1.2.3 322 4/17/2024
1.2.2 400 6/26/2023
1.2.1 451 3/7/2023
1.2.0 626 10/24/2022
1.1.9 579 10/4/2022
1.1.8 605 10/4/2022
1.1.7 598 9/30/2022
1.1.6 576 9/30/2022
1.1.5 670 5/24/2022
1.1.4 638 5/24/2022
1.1.3 667 5/10/2022
1.1.2 652 5/9/2022
1.1.1 826 11/5/2021
Loading failed

破坏性变更:移除 RunModeEnum,SetLog/SetLogSimple/SetLogWithoutDate 改为接收 LogOptions,输出级别与文件记录级别分离(文件级别不得低于输出级别),新增 FrameworkLevel(可空,null=不压制框架日志)与 SeqLevel 独立配置,提供 LogOptions.Production()/Development() 预设;升级后须重新编译消费方,net48 项目需重新生成绑定重定向