C#開發經驗之第三方庫
- 日志庫 - NLog
-
- 描述
- 使用方法
- Json解析庫 - Newtonsoft.Json
-
- 描述
- 使用方法
- 序列槽通信庫 - SerialPortStream
-
- 描述
- 使用方法
日志庫 - 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)
{
}
}
}