C# 开发经验之第三方库(持续更新,建议收藏)

日志库 - 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)
        {
        }
    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

车轮滚滚向西行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值