Skip to content

Commit eefeabf

Browse files
committed
Update logging
1 parent df2f178 commit eefeabf

File tree

9 files changed

+65
-56
lines changed

9 files changed

+65
-56
lines changed

source/TS.NET.Engine/Engine Tasks/HardwareThread.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ internal class HardwareThread : IEngineTask
1515
private CancellationTokenSource? cancelTokenSource;
1616
private Task? taskLoop;
1717

18-
public HardwareThread(ILoggerFactory loggerFactory,
18+
public HardwareThread(ILogger logger,
1919
ThunderscopeSettings settings,
2020
IThunderscope thunderscope,
2121
BlockingPool<DataDto> hardwarePool,
2222
BlockingRequestResponse<HardwareRequestDto, HardwareResponseDto> hardwareControl)
2323
{
24-
logger = loggerFactory.CreateLogger(nameof(HardwareThread));
24+
this.logger = logger;
2525
this.settings = settings;
2626
this.thunderscope = thunderscope;
2727
this.hardwarePool = hardwarePool;

source/TS.NET.Engine/Engine Tasks/PreProcessingThread.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class PreProcessingThread : IEngineTask
1313
private Task? taskLoop;
1414

1515
public PreProcessingThread(
16-
ILoggerFactory loggerFactory,
16+
ILogger logger,
1717
ThunderscopeSettings settings,
1818
BlockingPool<DataDto> hardwarePool,
1919
BlockingPool<DataDto> preProcessingPool)
2020
{
21-
logger = loggerFactory.CreateLogger(nameof(PreProcessingThread));
21+
this.logger = logger;
2222
this.settings = settings;
2323
this.hardwarePool = hardwarePool;
2424
this.preProcessingPool = preProcessingPool;

source/TS.NET.Engine/Engine Tasks/ProcessingThread.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ProcessingThread : IEngineTask
1818
private Task? taskLoop;
1919

2020
public ProcessingThread(
21-
ILoggerFactory loggerFactory,
21+
ILogger logger,
2222
ThunderscopeSettings settings,
2323
ThunderscopeHardwareConfig hardwareConfig,
2424
BlockingPool<DataDto> preProcessingPool,
@@ -27,7 +27,7 @@ public ProcessingThread(
2727
BlockingChannelWriter<INotificationDto>? uiNotifications,
2828
CaptureCircularBuffer captureBuffer)
2929
{
30-
logger = loggerFactory.CreateLogger(nameof(ProcessingThread));
30+
this.logger = logger;
3131
this.settings = settings;
3232
this.hardwareConfig = hardwareConfig;
3333
this.preProcessingPool = preProcessingPool;

source/TS.NET.Engine/Engine Tasks/ScpiServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class ScpiServer : TcpServer, IEngineTask
1313
private readonly BlockingRequestResponse<HardwareRequestDto, HardwareResponseDto> hardwareControl;
1414
private readonly BlockingRequestResponse<ProcessingRequestDto, ProcessingResponseDto> processingControl;
1515

16-
public ScpiServer(ILoggerFactory loggerFactory,
16+
public ScpiServer(ILogger logger,
1717
ThunderscopeSettings settings,
1818
IPAddress address,
1919
int port,
2020
BlockingRequestResponse<HardwareRequestDto, HardwareResponseDto> hardwareControl,
2121
BlockingRequestResponse<ProcessingRequestDto, ProcessingResponseDto> processingControl) : base(address, port)
2222
{
23-
logger = loggerFactory.CreateLogger(nameof(ScpiServer));
23+
this.logger = logger;
2424
this.settings = settings;
2525
this.hardwareControl = hardwareControl;
2626
this.processingControl = processingControl;

source/TS.NET.Engine/EngineManager.cs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Microsoft.Extensions.Configuration;
22
using Microsoft.Extensions.Logging;
3-
using NReco.Logging.File;
4-
using System.IO;
53
using System.Runtime.InteropServices;
64
using System.Runtime.Intrinsics.Arm;
75
using System.Runtime.Intrinsics.X86;
@@ -17,6 +15,9 @@ namespace TS.NET.Engine
1715
// By serialising the config-update/data-read it also allows for specific behaviours (like pausing acquisition on certain config updates) and ensuring a perfect match between sample-block & hardware configuration that created it.
1816
public class EngineManager
1917
{
18+
private readonly ILoggerFactory loggerFactory;
19+
private readonly ILogger logger;
20+
2021
private ThunderscopeSettings? thunderscopeSettings = null;
2122
private IThunderscope? thunderscope = null;
2223

@@ -28,6 +29,12 @@ public class EngineManager
2829

2930
public BlockingChannel<INotificationDto>? UiNotifications;
3031

32+
public EngineManager(ILoggerFactory loggerFactory)
33+
{
34+
this.loggerFactory = loggerFactory;
35+
logger = loggerFactory.CreateLogger(nameof(EngineManager));
36+
}
37+
3138
public bool TryStart(string configurationFile, string calibrationFile, string deviceSerial)
3239
{
3340
Console.CancelKeyPress += (sender, e) => { StopLibtslitex(); Environment.Exit(0); }; // Handle Ctrl+C or Ctrl+Break event.
@@ -40,27 +47,9 @@ public bool TryStart(string configurationFile, string calibrationFile, string de
4047
// Commented out for now, more testing needed on Windows
4148
//using FileStream fs = new(lockFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
4249

43-
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddJsonFile(
44-
"thunderscope-appsettings.json"
45-
);
46-
var configuration = configurationBuilder.Build();
4750
thunderscopeSettings = ThunderscopeSettings.FromYamlFile(configurationFile);
4851
var thunderscopeCalibrationSettings = ThunderscopeCalibrationSettings.FromJsonFile(calibrationFile);
4952

50-
var loggerFactory = LoggerFactory.Create(configure =>
51-
{
52-
configure
53-
.ClearProviders()
54-
.AddConfiguration(configuration.GetSection("Logging"))
55-
.AddFile(configuration.GetSection("Logging"))
56-
.AddSimpleConsole(options =>
57-
{
58-
options.SingleLine = true;
59-
options.TimestampFormat = "HH:mm:ss ";
60-
});
61-
});
62-
var logger = loggerFactory.CreateLogger("TS.NET.Engine");
63-
6453
if (RuntimeInformation.ProcessArchitecture == Architecture.X86 || RuntimeInformation.ProcessArchitecture == Architecture.X64)
6554
{
6655
if (!Avx2.IsSupported)
@@ -172,7 +161,7 @@ public bool TryStart(string configurationFile, string calibrationFile, string de
172161

173162
startSemaphore.Wait();
174163
processingThread = new ProcessingThread(
175-
loggerFactory: loggerFactory,
164+
logger: loggerFactory.CreateLogger(nameof(ProcessingThread)),
176165
settings: thunderscopeSettings,
177166
hardwareConfig: thunderscope.GetConfiguration(),
178167
preProcessingPool: preProcessingPool,
@@ -184,15 +173,15 @@ public bool TryStart(string configurationFile, string calibrationFile, string de
184173

185174
startSemaphore.Wait();
186175
preProcessingThread = new PreProcessingThread(
187-
loggerFactory: loggerFactory,
176+
logger: loggerFactory.CreateLogger(nameof(PreProcessingThread)),
188177
settings: thunderscopeSettings,
189178
hardwarePool: hardwarePool,
190179
preProcessingPool: preProcessingPool);
191180
preProcessingThread.Start(startSemaphore);
192181

193182
startSemaphore.Wait();
194183
hardwareThread = new HardwareThread(
195-
loggerFactory: loggerFactory,
184+
logger: loggerFactory.CreateLogger(nameof(HardwareThread)),
196185
settings: thunderscopeSettings,
197186
thunderscope: thunderscope,
198187
hardwarePool: hardwarePool,
@@ -201,7 +190,7 @@ public bool TryStart(string configurationFile, string calibrationFile, string de
201190

202191
startSemaphore.Wait();
203192
scpiServer = new ScpiServer(
204-
loggerFactory,
193+
logger: loggerFactory.CreateLogger(nameof(ScpiServer)),
205194
thunderscopeSettings,
206195
System.Net.IPAddress.Any,
207196
5025,
@@ -213,7 +202,7 @@ public bool TryStart(string configurationFile, string calibrationFile, string de
213202
switch (thunderscopeSettings.WaveformBufferReader)
214203
{
215204
case "DataServer":
216-
DataServer dataServer = new(loggerFactory, thunderscopeSettings, System.Net.IPAddress.Any, 5026, captureBuffer);
205+
DataServer dataServer = new(loggerFactory.CreateLogger(nameof(DataServer)), thunderscopeSettings, System.Net.IPAddress.Any, 5026, captureBuffer);
217206
waveformBufferReader = dataServer;
218207
break;
219208
case "None":

source/TS.NET.Engine/Program.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.CommandLine;
1+
using Microsoft.Extensions.Logging;
2+
using Serilog;
3+
using Serilog.Sinks.SystemConsole.Themes;
4+
using System.CommandLine;
25
using System.Text.Json;
36
using TS.NET;
47
using TS.NET.Engine;
@@ -54,7 +57,35 @@ static void Start(int deviceIndex, string configurationFile, string calibrationF
5457
File.WriteAllText("thunderscope-calibration (defaults).json", json);
5558
#endif
5659

57-
var engine = new EngineManager();
60+
//IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddJsonFile("thunderscope-appsettings.json");
61+
//var configuration = configurationBuilder.Build();
62+
63+
#if DEBUG
64+
var serilog = new LoggerConfiguration()
65+
.MinimumLevel.Debug()
66+
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
67+
.Enrich.FromLogContext()
68+
.WriteTo.Console(
69+
theme: AnsiConsoleTheme.Code,
70+
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:w4}] {SourceContext} {Message:lj}{NewLine}{Exception}"
71+
)
72+
.WriteTo.File("Logs/TS.NET.Engine .txt", rollingInterval: RollingInterval.Day)
73+
.CreateLogger();
74+
#else
75+
var serilog = new LoggerConfiguration()
76+
.MinimumLevel.Information()
77+
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
78+
.Enrich.FromLogContext()
79+
.WriteTo.Console(
80+
theme: AnsiConsoleTheme.Code,
81+
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:w4}] {SourceContext} {Message:lj}{NewLine}{Exception}"
82+
)
83+
.CreateLogger();
84+
#endif
85+
var loggerFactory = new LoggerFactory().AddSerilog(serilog);
86+
var logger = loggerFactory.CreateLogger("TS.NET.Engine");
87+
88+
var engine = new EngineManager(loggerFactory);
5889
var deviceSerial = deviceIndex.ToString();
5990
if (!engine.TryStart(configurationFile, calibrationFile, deviceSerial))
6091
return;

source/TS.NET.Engine/TS.NET.Engine.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
2323
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
2424
<PackageReference Include="NetCoreServer" Version="8.0.7" />
25-
<PackageReference Include="NReco.Logging.File" Version="1.2.1" />
2625
<PackageReference Include="Riok.Mapperly" Version="4.2.1" />
26+
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.2" />
27+
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
28+
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
2729
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
2830
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.2.0" />
2931
<PackageReference Include="YamlDotNet" Version="16.2.0" />
@@ -37,9 +39,6 @@
3739
<None Update="thunderscope-calibration.json">
3840
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3941
</None>
40-
<None Update="thunderscope-appsettings.json">
41-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
42-
</None>
4342
<None Update="thunderscope.json">
4443
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
4544
</None>

source/TS.NET.Engine/Waveform Buffer Readers/DataServer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ internal class DataServer : TcpServer, IEngineTask
1414
private readonly CancellationTokenSource cancellationTokenSource;
1515
private readonly ICaptureBufferReader captureBuffer;
1616

17-
public DataServer(ILoggerFactory loggerFactory, ThunderscopeSettings settings, IPAddress address, int port, ICaptureBufferReader captureBuffer) : base(address, port)
17+
public DataServer(
18+
ILogger logger,
19+
ThunderscopeSettings settings,
20+
IPAddress address,
21+
int port,
22+
ICaptureBufferReader captureBuffer) : base(address, port)
1823
{
19-
logger = loggerFactory.CreateLogger(nameof(DataServer));
24+
this.logger = logger;
2025
cancellationTokenSource = new();
2126
this.captureBuffer = captureBuffer;
2227
logger.LogDebug("Started");

source/TS.NET.Engine/thunderscope-appsettings.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)