From d22b07c2f5ce78d5ae8ffd11497b32881a312790 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Mon, 20 Jan 2020 14:58:31 +0100 Subject: [PATCH] Fixed nullable logger issue --- Sharp7.Rx/Interfaces/IPlc.cs | 2 ++ Sharp7.Rx/Sharp7Plc.cs | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Sharp7.Rx/Interfaces/IPlc.cs b/Sharp7.Rx/Interfaces/IPlc.cs index ad83374..e5c4c89 100644 --- a/Sharp7.Rx/Interfaces/IPlc.cs +++ b/Sharp7.Rx/Interfaces/IPlc.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Sharp7.Rx.Enums; namespace Sharp7.Rx.Interfaces @@ -10,5 +11,6 @@ namespace Sharp7.Rx.Interfaces Task SetValue(string variableName, TValue value); Task GetValue(string variableName); IObservable ConnectionState { get; } + ILogger Logger { get; } } } diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index abec30e..4f46aa4 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -49,10 +49,11 @@ namespace Sharp7.Rx } public IObservable ConnectionState { get; private set; } + public ILogger Logger { get; set; } public async Task InitializeAsync() { - s7Connector = new Sharp7Connector(plcConnectionSettings, varaibleNameParser); + s7Connector = new Sharp7Connector(plcConnectionSettings, varaibleNameParser){Logger = Logger}; ConnectionState = s7Connector.ConnectionState; await s7Connector.InitializeAsync(); @@ -66,7 +67,7 @@ namespace Sharp7.Rx } catch (Exception e) { - s7Connector.Logger.LogError(e, "Error while connecting to PLC"); + Logger?.LogError(e, "Error while connecting to PLC"); } }); #pragma warning restore 4014 @@ -387,7 +388,7 @@ namespace Sharp7.Rx .Select(states => states == Enums.ConnectionState.Connected) .SelectMany(connected => GetAllValues(connected, connector)) .RepeatAfterDelay(cycle) - .LogAndRetryAfterDelay(s7Connector.Logger, cycle, "Error while getting batch notifications from plc") + .LogAndRetryAfterDelay(Logger, cycle, "Error while getting batch notifications from plc") .TakeUntil(disposingSubject) .Subscribe(); } @@ -430,7 +431,7 @@ namespace Sharp7.Rx var min = performanceCoutner.Min(); var max = performanceCoutner.Max(); - s7Connector.Logger.LogInformation("Performance statistic during {0} elements of plc notification. Min: {1}, Max: {2}, Average: {3}, Plc: '{4}', Number of variables: {5}, Batch size: {6}", performanceCoutner.Capacity, min, max, average, plcConnectionSettings.IpAddress, multiVariableSubscriptions.ExistingKeys.Count(), + Logger?.LogInformation("Performance statistic during {0} elements of plc notification. Min: {1}, Max: {2}, Average: {3}, Plc: '{4}', Number of variables: {5}, Batch size: {6}", performanceCoutner.Capacity, min, max, average, plcConnectionSettings.IpAddress, multiVariableSubscriptions.ExistingKeys.Count(), MultiVarRequestMaxItems); performanceCoutner.Clear(); }