Snet.Log
26.166.2
dotnet add package Snet.Log --version 26.166.2
NuGet\Install-Package Snet.Log -Version 26.166.2
<PackageReference Include="Snet.Log" Version="26.166.2" />
<PackageVersion Include="Snet.Log" Version="26.166.2" />
<PackageReference Include="Snet.Log" />
paket add Snet.Log --version 26.166.2
#r "nuget: Snet.Log, 26.166.2"
#:package Snet.Log@26.166.2
#addin nuget:?package=Snet.Log&version=26.166.2
#tool nuget:?package=Snet.Log&version=26.166.2
<h1 align="center">Snet - 工业协议与数据采集框架</h1>
<p align="center"> <img width="120" height="120" src="https://api.shunnet.top/pic/nuget.png" alt="Snet Logo"/> </p>
<p align="center"> <b>统一 · 高效 · 灵活 · 可扩展</b> </p>
<p align="center">
<img src="https://img.shields.io/badge/.NET-8.0%20%7C%2010.0-purple.svg"/> <img src="https://img.shields.io/badge/NuGet-Available-brightgreen.svg"/> <img src="https://img.shields.io/badge/license-MIT-green"/> <img src="https://img.shields.io/badge/async-default-blue"/>
</p>
<p align="center"> 面向工业数据采集、传输、转发、消息中间件的全栈式解决方案 </p>
<p align="center">
<a href="https://shunnet.top/"><b>🚀 官网</b></a> · <a href="https://www.nuget.org/profiles/shun"><b>📚 NuGet</b></a> · <a href="https://github.com/shunnet"><b>💻 GitHub</b></a> · <a href="https://github.com/shunnet/Daq"><b>🔌 Daq 工具</b></a> <a href="https://github.com/shunnet/SKILLS"><b>📦 SKILLS</b></a>
</p>
✨ 框架特色
<table> <tr><td>⚡</td><td><b>异步优先架构</b></td><td>底层全部异步实现(<code>*Async</code> + <code>CancellationToken</code>),同步方法为 <code>.GetAwaiter().GetResult()</code> 薄封装</td></tr> <tr><td>📐</td><td><b>统一读写参数模型</b></td><td>入参出参结构统一,协议切换零改造,降低学习与迁移成本</td></tr> <tr><td>📡</td><td><b>多点转发 & 多点解析</b></td><td>单次采集可同时转发至多个 MQ 目标,支持 <code>AddressParseParam</code> 反射解析二次加工</td></tr> <tr><td>⚡</td><td><b>即插即用</b></td><td>NuGet 安装即用,最少 3 行代码启动一条采集链路</td></tr> <tr><td>🎭</td><td><b>虚拟点位 & 模拟库</b></td><td>内置 5 种虚拟地址类型与 Sim 模拟协议,无需真实设备即可开发调试</td></tr> <tr><td>🔔</td><td><b>全协议异步订阅</b></td><td>所有采集协议支持订阅式数据推送,<code>SubscribeOperate</code> 异步轮询(<code>await Task.Delay</code>)</td></tr> <tr><td>🧬</td><td><b>采集 & 转发接口统一</b></td><td><code>IDaq</code> / <code>IMq</code> / <code>ICommunication</code> 统一抽象,采集与转发共享同一套 API 范式</td></tr> <tr><td>🎯</td><td><b>双通道事件体系</b></td><td>同步(<code>OnDataEvent</code>)+ 异步(<code>OnDataEventAsync</code>)事件双通道,<code>EventingWrapperAsync<T></code> 顺序执行</td></tr> <tr><td>🌐</td><td><b>内置 WebAPI 控制</b></td><td>每个采集协议自带 HTTP API(<code>/api/on</code>、<code>/api/read</code>、<code>/api/write</code> 等 6 个端点),远程控制开关、读写、状态查询</td></tr> <tr><td>🧩</td><td><b>极简二次开发</b></td><td>继承 <code>DaqAbstract</code> / <code>MqAbstract</code> 实现 8 个抽象异步方法即可扩展新协议</td></tr> <tr><td>📝</td><td><b>高可定制日志</b></td><td>6 级日志(Verbose → Fatal),自定义路径、数据库输出、控制台开关</td></tr> <tr><td>🌍</td><td><b>中英文实时切换</b></td><td>全局多语言支持,运行时热切换,<code>OnLanguageEvent</code> 事件驱动自动刷新</td></tr> <tr><td>🔧</td><td><b>丰富内置能力</b></td><td>反射解析 · 进程缓存 · 共享内存缓存 · Channel 通道 · 字节自动转换 · 单例工厂</td></tr> <tr><td>📦</td><td><b>多格式序列化</b></td><td>JSON / XML / Protobuf 等一站式序列化与反序列化支持</td></tr> <tr><td>🏎️</td><td><b>高性能内核</b></td><td>无 GC.Collect 阻塞 · O(1) 字典查找 · Channel 背压控制 · 零分配热路径</td></tr> </table>
🧩 核心组件
| 模块 | 说明 | 关键能力 |
|---|---|---|
| Snet.Log | 日志系统 | Verbose · Debug · Info · Warning · Error · Fatal,支持文件/数据库/控制台多通道输出 |
| Snet.Utility | 公共方法集 | 字节处理 · 枚举 · 文件 · 字符串 · 验证 · 比对 · 转换 · 反射 · JSON · XML · Protobuf · FTP · System |
| Snet.Model | 数据模型层 | 特性 · 数据结构 · 枚举(DataType/QualityType/AddressType/DataFormat/EncodingType) · 接口(IDaq/IMq/ICommunication) |
| Snet.Core | 核心引擎 | DaqAbstract/MqAbstract/CommunicationAbstract · 通信(TCP Client+Service/UDP/HTTP Client+Service/WS Client+Service/串口) · SubscribeOperate · ChannelOperate · ProcessCacheOperate/ShareCacheOperate · ReflectionOperate · AddressHandler · BytesHandler · VirtualAddressManage · WebAPI |
📡 采集协议(30+ NuGet 包)
NuGet 包(独立项目,可单独安装)
| 包名 | 协议 | Operate 类 | ProtocolType 数量 |
|---|---|---|---|
Snet.Siemens |
西门子 S7/PPI/S7Plus/FetchWrite | SiemensOperate |
10 种 |
Snet.Modbus |
Modbus TCP/UDP/RTU/ASCII/RTUoTCP/ASCIIoTCP | ModbusOperate |
6 种 |
Snet.Mitsubishi |
三菱 MC/FX/A1E/A3C/CIP/Links | MitsubishiOperate |
14 种 |
Snet.Omron |
欧姆龙 Fins/CIP/HostLink/CMode | OmronOperate |
8 种 |
Snet.Inovance |
汇川 TCP/Serial/CIP/Easy/ComputerLink | InovanceOperate |
6 种 |
Snet.Opc |
OPC UA Client+Server / DA Client / DA HTTP | OpcUaClientOperate / OpcUaServiceOperate / OpcDaClientOperate / OpcDaHttpOperate |
4 个类 |
Snet.AllenBradley |
罗克韦尔 CIP/PCCC/SLC/DF1/Micro800 | AllenBradleyOperate |
6 种 |
Snet.Delta |
台达 TCP/Serial/ASCII | DeltaOperate |
5 种 |
Snet.Keyence |
基恩士 MC/Nano/KvOld | KeyenceOperate |
5 种 |
Snet.Panasonic |
松下 MC/Mewtocol | PanasonicOperate |
3 种 |
Snet.Beckhoff |
倍福 ADS | BeckhoffOperate |
1 种 |
Snet.GE |
通用电气 SRTP | GEOperate |
1 种 |
Snet.Yaskawa |
安川 Memobus TCP/UDP | YaskawaOperate |
2 种 |
Snet.Invt |
英威腾(Modbus) | InvtOperate |
2 种 |
Snet.MegMeet |
麦格米特 TCP/Serial | MegMeetOperate |
3 种 |
Snet.Cimon |
西蒙 PLC | CimonOperate |
1 种 |
Snet.Fanuc |
发那科 CNC/机器人 | FanucOperate |
1 种 |
Snet.Fatek |
永宏 PLC | FatekOperate |
2 种 |
Snet.Fuji |
富士 SPH/SPB | FujiOperate |
4 种 |
Snet.LSis |
LS 产电 XGT/Cnet | LSisOperate |
4 种 |
Snet.RKC |
理化温控器 | RKCOperate |
2 种 |
Snet.Toyota |
丰田机器人 | ToyotaOperate |
1 种 |
Snet.Turck |
图尔克 IO-Link/ReaderNet | TurckOperate |
1 种 |
Snet.Vigor |
丰炜 PLC | VigorOperate |
2 种 |
Snet.WeCon |
维控 PLC (Modbus) | WeConOperate |
2 种 |
Snet.XinJE |
信捷 PLC | XinJEOperate |
4 种 |
Snet.Yamatake |
山武/AZBIL Digitron CPL | YamatakeOperate |
2 种 |
Snet.Yokogawa |
横河 PLC | YokogawaOperate |
1 种 |
Snet.PQDIF |
电力通讯规约(DLT645/DLT698/CJT188/DTSU6606) | PQDIFOperate |
10 种 |
Snet.DB |
数据库采集(SqlServer/MySQL/Oracle/SQLite) | DBOperate |
4 种 DB |
Snet.TEP |
TCP 扩展插件(非标设备采集) | TepMasterOperate / TepSlaveOperate |
自定义 |
Snet.Freedom |
自由协议(自定义报文) | FreedomOperate |
3 种 |
Snet.Sim |
模拟库(无硬件测试) | SimOperate |
5 种虚拟地址 |
接口定义
/// <summary>
/// 数采接口(聚合 14 个基础接口,含同步 + 异步方法)
/// </summary>
public interface IDaq : IOn, IOff, IRead, IWrite, ISubscribe, IGetStatus, IEvent,
IGetParam, ICreateInstance, ILog, IWA, IGetObject, ILanguage,
IDisposable, IAsyncDisposable { }
异步方法:
OnAsync/OffAsync/ReadAsync/WriteAsync/SubscribeAsync/UnSubscribeAsync/GetStatusAsync/GetBaseObjectAsync— 所有方法均接受CancellationToken token = default
📬 消息中间件协议
支持 5 种高性能消息中间件:
| 中间件 | NuGet 包 | 能力 | Operate 类 | ISns 格式 |
|---|---|---|---|---|
| MQTT | Snet.Mqtt |
Client + Service + WSService | MqttClientOperate / MqttServiceOperate / MqttWebSocketServiceOperate |
Snet.Mqtt.client.MqttClientOperate.{SN} |
| Kafka | Snet.Kafka |
AdminClient · Producer · Consumer | KafkaOperate |
Snet.Kafka.KafkaOperate.{SN} |
| RabbitMQ | Snet.RabbitMQ |
Publish · Subscribe | RabbitMQOperate |
Snet.RabbitMQ.RabbitMQOperate.{SN} |
| NetMQ | Snet.NetMQ |
Publish · Subscribe | NetMQOperate |
Snet.NetMQ.NetMQOperate.{SN} |
| Netty | Snet.Netty |
Client + Service | NettyClientOperate / NettyServiceOperate |
Snet.Netty.client.NettyClientOperate.{SN} |
接口定义
/// <summary>
/// 消息中间件接口(异步优先)
/// </summary>
public interface IMq : IOn, IOff, IProducer, IConsumer, IGetStatus, IEvent,
IGetParam, ICreateInstance, ILog, ILanguage, IDisposable, IAsyncDisposable { }
异步方法:
OnAsync/OffAsync/ProduceAsync/ConsumeAsync/UnConsumeAsync
🖥️ 协议服务端 (数据模拟)
| 服务端 | NuGet 包 | Operate 类 |
|---|---|---|
| MQTT Service | Snet.Mqtt |
MqttServiceOperate |
| MQTT WS Service | Snet.Mqtt |
MqttWebSocketServiceOperate |
| OPC UA Service | Snet.Opc |
OpcUaServiceOperate |
| TCP Service | Snet.Core |
TcpServiceOperate |
| WebSocket Service | Snet.Core |
WsServiceOperate |
| HTTP Service | Snet.Core |
HttpServiceOperate |
⚡ 实例创建方式
支持 无参、有参、单例、接口化实例 等多种方式,快速上手:
// 实例创建的几种方式(以 OPC UA 采集协议为例)
using Snet.Model.@interface;
using Snet.Opc.ua.client;
OpcUaClientOperate? operate = null;
IDaq? daq = null;
// 无参实例
operate = new OpcUaClientOperate();
// 无参实例调函数创建实例
operate = new OpcUaClientOperate()
.CreateInstance(new OpcUaClientData.Basics())
.GetSource<OpcUaClientOperate>();
// 有参实例
operate = new OpcUaClientOperate(new OpcUaClientData.Basics());
// 有参单例
operate = OpcUaClientOperate.Instance(new OpcUaClientData.Basics());
// 接口 - 有参实例
daq = new OpcUaClientOperate(new OpcUaClientData.Basics());
// 接口 - 有参单例
daq = OpcUaClientOperate.Instance(new OpcUaClientData.Basics());
// 使用 async/await(推荐)
await using (operate)
{
await operate.OnAsync();
// ... 采集操作 ...
await operate.OffAsync();
}
📥 采集应用示例(异步)
// 采集协议 — OPC UA(异步)
using System.Collections.Concurrent;
using Snet.Log;
using Snet.Model.data;
using Snet.Model.@enum;
using Snet.Opc.ua.client;
using Snet.Utility;
// .NET 顶级语句直接支持 await
var basics = new OpcUaClientData.Basics
{
ServerUrl = "opc.tcp://127.0.0.1:6688",
UserName = "user",
Password = "password",
};
await using var operate = new OpcUaClientOperate(basics);
// 点位地址
Address address = new Address
{
SN = Guid.NewGuid().ToString(),
CreationTime = DateTime.Now,
AddressArray = new List<AddressDetails>
{
new AddressDetails
{
SN = "机台1", // 唯一标识符(机台号/组名/车间/厂)
AddressName = "ns=2;s=MyVariable", // 实际地址 [ 不能为空 ]
AddressDataType = DataType.Float, // 数据类型
AddressAnotherName = "温度", // 地址别名
AddressDescribe = "产线1温度传感器", // 地址描述
AddressType = AddressType.Reality, // 地址类型
IsEnable = true, // 是否启用
AddressMqParam = new AddressMq // 消息队列自动转发
{
ISns = new List<string> { "Snet.Mqtt.client.MqttClientOperate.my-mqtt" },
Topic = "factory/temperature",
ContentFormat = "温度 = {0}"
},
AddressParseParam = new AddressParse // 反射解析(二次加工)
{
ReflectionParam = new object[]
{
new ReflectionData.Basics { /* 反射解析配置 */ },
"MyParseMethodSN"
}
},
}
}
};
#region 打开
OperateResult result = await operate.OnAsync();
LogHelper.Info(result.ToJson(true));
#endregion
#region 读取
result = await operate.ReadAsync(address);
if (result.GetDetails<ConcurrentDictionary<string, AddressValue>>(out var data))
{
foreach (var kv in data)
{
if (kv.Value.Quality == QualityType.Normal)
LogHelper.Info($"{kv.Key} = {kv.Value.ResultValue}");
}
}
#endregion
#region 订阅
// 异步信息事件
operate.OnInfoEventAsync += async (sender, e) =>
{
if (!e.Status)
LogHelper.Warning($"告警: {e.Message}");
await Task.CompletedTask;
};
// 异步数据事件
operate.OnDataEventAsync += async (sender, e) =>
{
if (!e.Status) return;
var d = e.GetSource<ConcurrentDictionary<string, AddressValue>>();
if (d == null) return;
// 精简版数据(轻量传输,Protobuf 兼容)
var simplifies = d.GetSimplifyArray();
// 异步处理(写数据库/HTTP 转发等)
await ProcessDataAsync(simplifies);
};
result = await operate.SubscribeAsync(address);
LogHelper.Info($"订阅已启动: {(result.Status ? "成功" : result.Message)}");
#endregion
#region 写入
var values = new ConcurrentDictionary<string, object>
{
["ns=2;s=TargetValue"] = (float)25.6,
["ns=2;s=Enable"] = true
};
result = await operate.WriteAsync(values);
LogHelper.Info(result.ToJson(true));
#endregion
#region 获取状态
result = await operate.GetStatusAsync();
LogHelper.Info($"连接状态: {(result.Status ? "已连接" : "未连接")}");
#endregion
#region 获取参数
result = operate.GetParam();
LogHelper.Info(result.GetSource<ParamModel>().ToJson(true));
#endregion
#region 关闭
await operate.OffAsync();
#endregion
🔄 MQ 应用示例(异步)
// MQ 协议 — MQTT(异步)
using Snet.Log;
using Snet.Model.data;
using Snet.Model.@enum;
using Snet.Mqtt.client;
using Snet.Utility;
var basics = new MqttClientData.Basics
{
SN = "my-mqtt", // 重要:用于 ISns 匹配
IpAddress = "127.0.0.1", // ← 属性名是 IpAddress,不是 Ip!
Port = 1883, // 库默认 6688,需显式指定标准端口
UserName = "user",
Password = "password",
ClientID = null, // null 则自动生成随机 ID
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
ResponseType = ResponseType.Content,
};
await using var operate = new MqttClientOperate(basics);
#region 打开
OperateResult result = await operate.OnAsync();
LogHelper.Info($"连接 MQTT: {(result.Status ? "成功" : result.Message)}");
#endregion
#region 生产
result = await operate.ProduceAsync("topic/hello", "hello world");
LogHelper.Info(result.ToJson(true));
#endregion
#region 消费
// 异步信息事件
operate.OnInfoEventAsync += async (sender, e) =>
{
if (!e.Status)
LogHelper.Warning($"MQTT 异常: {e.Message}");
await Task.CompletedTask;
};
// 异步数据事件
operate.OnDataEventAsync += async (sender, e) =>
{
if (!e.Status) return;
// 根据 ResponseType 判断数据类型
switch (basics.ResponseType)
{
case ResponseType.Bytes:
byte[] bytes = e.GetSource<byte[]>();
break;
case ResponseType.Content:
string str = e.GetSource<string>();
break;
case ResponseType.ContentWithTopic:
ResponseModel rm = e.GetSource<ResponseModel>();
// rm.Topic = 主题, rm.Content = 内容
break;
}
};
result = await operate.ConsumeAsync("topic/hello");
LogHelper.Info($"订阅: {(result.Status ? "成功" : result.Message)}");
#endregion
#region 关闭 & 状态
result = await operate.GetStatusAsync();
LogHelper.Info($"状态: {(result.Status ? "已连接" : "未连接")}");
await operate.OffAsync();
#endregion
⚙️ 常用操作类名
| 类名 | 命名空间 | 用途 |
|---|---|---|
DaqAbstract<O, D> |
Snet.Core.abstract |
采集抽象基类(实现 8 个异步方法) |
MqAbstract<O, D> |
Snet.Core.abstract |
消息中间件抽象基类 |
CoreUnify<O, D> |
Snet.Core.extend |
统一内核(单例 · 事件 · 日志 · 语言 · 计时) |
TcpClientOperate |
Snet.Core.communication.net.tcp.client |
TCP 客户端(断线重连) |
TcpServiceOperate |
Snet.Core.communication.net.tcp.service |
TCP 服务端(多客户端管理) |
UdpOperate |
Snet.Core.communication.net.udp |
UDP 通信(广播/定向) |
SerialOperate |
Snet.Core.communication.serial |
串口通信 |
WsClientOperate |
Snet.Core.communication.net.ws.client |
WebSocket 客户端 |
WsServiceOperate |
Snet.Core.communication.net.ws.service |
WebSocket 服务端 |
HttpClientOperate |
Snet.Core.communication.net.http.client |
HTTP 客户端 |
HttpServiceOperate |
Snet.Core.communication.net.http.service |
HTTP 服务端(REST API) |
SubscribeOperate |
Snet.Core.subscription |
异步轮询订阅引擎 |
AddressHandler |
Snet.Core.handler |
地址处理管线(类型转换+反射解析+MQ转发) |
BytesHandler |
Snet.Core.handler |
字节数组自动解析(14 种类型 + 4 种字节序) |
ChannelOperate<T> |
Snet.Core.channel |
高性能异步数据管道(背压控制) |
ProcessCacheOperate |
Snet.Core.cache.process |
进程内存缓存(MemoryCache) |
ShareCacheOperate |
Snet.Core.cache.share |
跨进程共享内存缓存(MemoryMappedFile + Mutex) |
ReflectionOperate |
Snet.Core.reflection |
反射操作(动态加载 DLL/调用方法/注册事件) |
PluginOperate |
Snet.Core.plugin |
插件热加载(AssemblyLoadContext) |
VirtualAddressManage |
Snet.Core.virtualAddress |
虚拟地址管理(5 种模式) |
🤖 AI 辅助开发
本项目提供 Claude Code 技能集,支持自然语言生成采集代码和插件:
| 技能 | 说明 | 一句话示例 |
|---|---|---|
| 🔌 DAQ-Skill | 使用现有协议库完成采集+转发 | "连接西门子S7-1500,IP 192.168.0.1,读取DB1.0,MQTT转发" |
| 🧩 PluginDev-Skill | 开发自定义协议插件,打包 ZIP 部署 | "开发温湿度传感器插件,TCP 发送 Modbus 帧" |
📂 技能文件位于 SKILLS
🌍 相关链接
| 🔗 | 链接 | 说明 |
|---|---|---|
| 🌐 | 官网 | 官方网站 |
| 📚 | NuGet | NuGet 包仓库 |
| 💻 | GitHub | 源代码仓库 |
| 🔌 | Daq 工具 | 插件化数采工具 |
| 🤖 | SKILLS | Claude Code 技能集(AI 辅助开发) |
| 📝 | 博客 | 技术博客 |
📜 许可证
本项目基于 MIT 开源。请阅读 LICENSE 获取完整条款。 ⚠️ 软件按 "原样" 提供,作者不对使用后果承担责任。
🌍 查阅
👉 点击跳转
| Product | Versions 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. |
-
net10.0
- Serilog (>= 4.3.1)
- Serilog.Sinks.Console (>= 6.1.1)
- Serilog.Sinks.File (>= 7.0.0)
-
net8.0
- Serilog (>= 4.3.1)
- Serilog.Sinks.Console (>= 6.1.1)
- Serilog.Sinks.File (>= 7.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Snet.Log:
| Package | Downloads |
|---|---|
|
Snet.Model
核心组件:Model(特性、结构、模型、枚举、接口) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 26.166.2 | 2 | 6/15/2026 |
| 26.166.1 | 86 | 6/15/2026 |
| 26.155.1 | 1,024 | 6/4/2026 |
| 26.114.1 | 4,251 | 4/24/2026 |
| 26.110.1 | 863 | 4/20/2026 |
| 26.100.1 | 870 | 4/10/2026 |
| 26.99.1 | 883 | 4/9/2026 |
| 26.97.1 | 873 | 4/7/2026 |
| 26.83.1 | 939 | 3/24/2026 |
| 26.75.1 | 3,307 | 3/16/2026 |
| 26.59.1 | 947 | 2/28/2026 |
| 26.43.1 | 1,034 | 2/12/2026 |
| 26.41.1 | 893 | 2/10/2026 |
| 26.35.1 | 1,422 | 2/4/2026 |
| 26.15.1 | 1,668 | 1/15/2026 |
| 26.13.1 | 927 | 1/13/2026 |
| 25.357.1 | 1,577 | 12/23/2025 |
| 25.329.1 | 1,525 | 11/25/2025 |
| 25.322.1 | 2,853 | 11/18/2025 |
| 25.317.1 | 4,901 | 11/13/2025 |