Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove NLog Dependency #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[Rr]eleases/
[Bb]in/
[Oo]bj/

[Vv]s/
# NuGet
packages
!packages/repositories.config
Expand Down
24 changes: 12 additions & 12 deletions SerialPortLib/SerialPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ limitations under the License.
* Project Homepage: https://github.com/genielabs/serialport-lib-dotnet
*/

using Microsoft.Extensions.Logging;
using System;
using System.Threading;

using System.IO.Ports;
using System.Runtime.InteropServices;
using NLog;
using System.Threading;

namespace SerialPortLib
{
Expand Down Expand Up @@ -62,7 +61,7 @@ public class SerialPortInput

#region Private Fields

private static Logger _logger = LogManager.GetCurrentClassLogger();
private static ILogger _logger;
private SerialPort _serialPort;

private string _portName = "";
Expand Down Expand Up @@ -112,10 +111,11 @@ public class SerialPortInput

#region Public Members

public SerialPortInput()
public SerialPortInput(ILogger logger = default)
{
_connectionWatcherCts = new CancellationTokenSource();
_readerCts = new CancellationTokenSource();
_logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -214,7 +214,7 @@ public bool SendMessage(byte[] message)
{
_serialPort.Write(message, 0, message.Length);
success = true;
LogDebug(BitConverter.ToString(message));
LogDebug($"SEND: {BitConverter.ToString(message)}");
}
catch (Exception e)
{
Expand Down Expand Up @@ -321,7 +321,7 @@ private void HandleErrorReceived(object sender, SerialErrorReceivedEventArgs e)

private void ReaderTask(object data)
{
var ct = (CancellationToken) data;
var ct = (CancellationToken)data;
while (IsConnected && !ct.IsCancellationRequested)
{
int msglen = 0;
Expand Down Expand Up @@ -357,7 +357,7 @@ private void ReaderTask(object data)

private void ConnectionWatcherTask(object data)
{
var ct = (CancellationToken) data;
var ct = (CancellationToken)data;
// This task takes care of automatically reconnecting the interface
// when the connection is drop or if an I/O error occurs
while (!_disconnectRequested && !ct.IsCancellationRequested)
Expand Down Expand Up @@ -395,17 +395,17 @@ private void ConnectionWatcherTask(object data)

private void LogDebug(string message)
{
_logger.Debug(message);
_logger?.LogDebug(message);
}

private void LogError(Exception ex)
{
_logger.Error(ex, null);
_logger?.LogError(ex, null);
}

private void LogError(SerialError error)
{
_logger.Error("SerialPort ErrorReceived: {0}", error);
_logger?.LogError("SerialPort ErrorReceived: {0}", error);
}

#endregion
Expand All @@ -431,7 +431,7 @@ protected virtual void OnConnectionStatusChanged(ConnectionStatusChangedEventArg
/// <param name="args">Arguments.</param>
protected virtual void OnMessageReceived(MessageReceivedEventArgs args)
{
LogDebug(BitConverter.ToString(args.Data));
LogDebug($"RECEIVED: {BitConverter.ToString(args.Data)}");
if (MessageReceived != null)
{
MessageReceived(this, args);
Expand Down
8 changes: 4 additions & 4 deletions SerialPortLib/SerialPortLib.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>SerialPortLib</PackageId>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Generoso Martello</Authors>
<Company>G-Labs</Company>
<Description>Serial Port libray for .Net
Expand All @@ -16,7 +16,7 @@
<TargetFrameworks>net472;net6.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.0" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion TestApp.Net/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ static void SerialPort_ConnectionStatusChanged(object sender, ConnectionStatusCh
private static IServiceProvider BuildDi()
{
return new ServiceCollection()
.AddTransient<SerialPortInput>()
.AddTransient(sp =>
{
var logger = sp.GetRequiredService<ILogger<Program>>();
return new SerialPortInput(logger);
})
.AddLogging(loggingBuilder =>
{
// configure Logging with NLog
Expand Down