Skip to content

Commit 34a7bef

Browse files
committed
v1.2.0
- Replaced NLog with MEL (issue #32 and pr #33) - Improved ReaderTask speed using BaseStream (pr #8) - Using Timer for ConnectionWatcherTask instead of thread loop - Added *SerialPort* property to access the underlying *System.IO.Ports.SerialPort* instance directly (issue #34) - Added *Timeout* property to get/set read/write timeout - Fixed a few minor bugs - Upgraded to latest packages version
1 parent baee442 commit 34a7bef

File tree

10 files changed

+228
-139
lines changed

10 files changed

+228
-139
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66

77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12-
<PackageReference Include="NUnit" Version="3.13.2" />
13-
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
14-
<PackageReference Include="coverlet.collector" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
12+
<PackageReference Include="NUnit" Version="4.3.2" />
13+
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
1514
</ItemGroup>
1615

1716
</Project>

SerialPortLib.Tests/Tests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public class Tests
2626
[Test]
2727
public void Test1()
2828
{
29-
Assert.True(true);
29+
Assert.Pass();
3030
}
3131
}

SerialPortLib/Events.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This file is part of SerialPortLib (https://github.com/genielabs/serialport-lib-dotnet)
33
4-
Copyright (2012-2023) G-Labs (https://github.com/genielabs)
4+
Copyright (2012-2025) G-Labs (https://github.com/genielabs)
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

SerialPortLib/Logging.cs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.IO.Ports;
8+
using System.Runtime.CompilerServices;
9+
10+
namespace SerialPortLib
11+
{
12+
public static class Logging
13+
{
14+
private static readonly ILogger Logger;
15+
16+
static Logging()
17+
{
18+
var configuration = new ConfigurationBuilder()
19+
.SetBasePath(Directory.GetCurrentDirectory())
20+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
21+
.Build();
22+
23+
var serviceCollection = new ServiceCollection();
24+
serviceCollection.AddLogging(builder =>
25+
{
26+
builder.AddConfiguration(configuration.GetSection("Logging"));
27+
builder.AddSimpleConsole(options => configuration.GetSection("Logging:Console:FormatterOptions"));
28+
builder.AddConsole();
29+
});
30+
31+
var serviceProvider = serviceCollection.BuildServiceProvider();
32+
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
33+
Logger = loggerFactory.CreateLogger("SerialPortInput");
34+
}
35+
36+
public static void LogDebug(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
37+
{
38+
Logger.LogWithCallInfo(LogLevel.Debug, message, null, memberName, filePath, lineNumber);
39+
}
40+
41+
public static void LogError(Exception ex, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
42+
{
43+
Logger.LogWithCallInfo(LogLevel.Error, ex.Message, ex, memberName, filePath, lineNumber);
44+
}
45+
46+
public static void LogError(SerialError error, [CallerMemberName] string methodName = "")
47+
{
48+
Logger.LogError($"SerialPort error occurred in {methodName}: {error}");
49+
}
50+
51+
// Extension to add caller info
52+
private static void LogWithCallInfo(this ILogger logger, LogLevel logLevel, string message, Exception exception = null,
53+
[CallerMemberName] string memberName = "",
54+
[CallerFilePath] string filePath = "",
55+
[CallerLineNumber] int lineNumber = 0)
56+
{
57+
using (logger.BeginScope(new Dictionary<string, object>
58+
{
59+
{"CallerMemberName", memberName},
60+
{"CallerFilePath", filePath},
61+
{"CallerLineNumber", lineNumber}
62+
}))
63+
{
64+
logger.Log(logLevel, message, exception);
65+
}
66+
}
67+
}
68+
69+
}

0 commit comments

Comments
 (0)