日志库 - NLog
NLOG,简直不要太经典!
描述
NLog is a logging platform for .NET with rich log routing and management capabilities.
NLog supports traditional logging, structured logging and the combination of both.
Support
NLog supports the following platforms:
.NET 5
.NET Framework 3.5, 4, 4.5 - 4.8
.NET Framework 4 client profile
Xamarin Android
Xamarin iOs
Windows Phone 8
Silverlight 4 and 5
Mono 4
ASP.NET 4 (NLog.Web package)
ASP.NET Core (NLog.Web.AspNetCore package)
.NET Core (NLog.Extensions.Logging package)
.NET Standard 1.x - NLog 4.5
.NET Standard 2.x - NLog 4.5
UWP - NLog 4.5
For ASP.NET Core, check: https://www.nuget.org/packages/NLog.Web.AspNetCore
项目地址:https://nlog-project.org/
使用方法
第一步:
通过Nuget添加到项目,如果兼容建议使用最新版,而且整个工程中保持统一版本
第二步,引用,定义变量,打印日志:
using NLog;
...
/// <summary>
/// 日志操作对象
/// </summary>
Logger logger = LogManager.GetCurrentClassLogger();
...
打印日志
logger.Error("msg...");
第三步,修改Config文件,内容包括日志的输出规则,输出路径等待,具体参考官方介绍
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
-->
<target xsi:type="File" name="f" fileName="${basedir}/../Logs/${shortdate}/${shortdate}.log"
layout="${longdate} ${logger} ${uppercase:${level}} ${message} "
archiveAboveSize="5242880" archiveNumbering="Sequence" concurrentWrites="true" />
<target name="console" layout="${longdate} ${message} ${exception:format=tostring}" type="ColoredConsole"></target>
</targets>
<rules>
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
<!--TRACE,DEBUG,INFO,WARN,ERROR,FATAL-->
<logger name="*" minlevel="Debug" writeTo="f" />
<logger name="*" minlevel="Trace" writeTo="console" />
</rules>
</nlog>
Json解析库 - Newtonsoft.Json
描述
Json.NET is a popular high-performance JSON framework for .NET
项目地址: https://www.newtonsoft.com/json
使用方法
using Newtonsoft.Json;
//序列化为Json
string str = JsonConvert.SerializeObject("需要序列化的对象");
//反序列化
DataStructure data = JsonConvert.DeserializeObject<DataStructure >("反序列化的字符串");
串口通信库 - SerialPortStream
描述
An independent implementation of System.IO.Ports.SerialPort and SerialStream for better reliability and maintainability.
项目地址:https://github.com/jcurl/serialportstream
使用方法
using RJCP.IO.Ports;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UsbDemo
{
class ComConnect
{
protected SerialPortStream serialPort = null;
public ComConnect()
{
}
public void ConnectionCOM()
{
//配置串口参数
serialPort = new SerialPortStream("COM3", 115200, 8, Parity.None, StopBits.One);
serialPort.Handshake = Handshake.None;
serialPort.RtsEnable = true;
serialPort.DataReceived += OnReviceData;
serialPort.ErrorReceived += OnErrorReceived;
serialPort.PinChanged += OnPinChanged;
if (!serialPort.IsOpen)
{
//打开串口
serialPort.Open();
var data = Encoding.UTF8.GetBytes("test");
//同步发送
serialPort.Write(data, 0, data.Length);
//异步发送
serialPort.WriteAsync(data, 0, data.Length);
//关闭串口
serialPort.Close();
}
}
/// <summary>
/// 数据接收事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnReviceData(object sender, SerialDataReceivedEventArgs e)
{
}
/// <summary>
/// 针脚改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnPinChanged(object sender, SerialPinChangedEventArgs e)
{
}
/// <summary>
/// 异常信息事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
}
}
}