Dijing.SerilogExt 1.2.9

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

// 初始化(建议在程序入口调用一次)
InitLog.SetLog(RunModeEnum.Debug, path: "logs");

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

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

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

输出行为

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

Sink 接收级别 说明
Console Information 及以上 无控制台的进程(如 WinExe)输出会被系统丢弃,不抛异常
File Warning 及以上 按天滚动,默认 logs/log-.txt,单文件上限 10 MB,默认保留 30 个
Seq 视重载而定 仅带 seqUrl 参数的重载会挂载;Debug 模式全量,Release 模式 Warning 及以上
InMemorySink 全部级别 通过事件供上层订阅,用于把日志推到界面

注意:Information 级别不会写入文件。如果需要完整落盘,请自行追加一个不限级别的 File sink。

API

// 基础配置
InitLog.SetLog(RunModeEnum mode,
               int retainedFileCountLimit = 30,
               string path = "logs",
               long fileSize = 10485760);

// 带 Seq 的配置(自动附加 Application / MachineName 属性)
InitLog.SetLog(RunModeEnum mode,
               string applicationName,
               string machineName,
               string seqUrl,
               int retainedFileCountLimit = 30,
               string path = "logs",
               long fileSize = 10485760);

// 精简输出模板(时间只到秒)
InitLog.SetLogSimple(RunModeEnum mode, ...);

// 输出模板不含日期前缀
InitLog.SetLogWithoutDate(RunModeEnum mode, ...);

RunModeEnum.Debug 输出毫秒级时间戳并全量打印;RunModeEnum.Release 额外把 Microsoft / System 命名空间压到 Warning 级别,减少框架噪音。

此外,SetLog 会自动接管 AppDomain.UnhandledException 与 ProcessExit,未捕获异常与进程退出都会留下一条 Warning 记录。

把日志接到界面

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

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

.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 安装本包。

版本记录

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
1.2.9 0 11/10/2026
1.2.8 446 6/17/2025
1.2.7 341 5/26/2025
1.2.6 306 5/26/2025
1.2.5 292 5/19/2025
1.2.4 809 7/23/2024
1.2.3 320 4/17/2024
1.2.2 398 6/26/2023
1.2.1 449 3/7/2023
1.2.0 624 10/24/2022
1.1.9 575 10/4/2022
1.1.8 603 10/4/2022
1.1.7 596 9/30/2022
1.1.6 574 9/30/2022
1.1.5 668 5/24/2022
1.1.4 636 5/24/2022
1.1.3 663 5/10/2022
1.1.2 649 5/9/2022
1.1.1 824 11/5/2021
1.1.0 528 11/1/2021
Loading failed

日志扩展;Serilog 升级至 4.4.0、Serilog.Sinks.Seq 升级至 9.1.0。注意:Serilog 程序集版本由 4.3.0.0 变为 4.4.0.0,net48 项目需重新生成绑定重定向(删除手写的 bindingRedirect,交给 AutoGenerateBindingRedirects)