Fixed nullable logger issue

This commit is contained in:
Federico Barresi
2020-01-20 14:58:31 +01:00
parent 412a460261
commit d22b07c2f5
2 changed files with 7 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
namespace Sharp7.Rx.Interfaces namespace Sharp7.Rx.Interfaces
@@ -10,5 +11,6 @@ namespace Sharp7.Rx.Interfaces
Task SetValue<TValue>(string variableName, TValue value); Task SetValue<TValue>(string variableName, TValue value);
Task<TValue> GetValue<TValue>(string variableName); Task<TValue> GetValue<TValue>(string variableName);
IObservable<ConnectionState> ConnectionState { get; } IObservable<ConnectionState> ConnectionState { get; }
ILogger Logger { get; }
} }
} }

View File

@@ -49,10 +49,11 @@ namespace Sharp7.Rx
} }
public IObservable<ConnectionState> ConnectionState { get; private set; } public IObservable<ConnectionState> ConnectionState { get; private set; }
public ILogger Logger { get; set; }
public async Task<bool> InitializeAsync() public async Task<bool> InitializeAsync()
{ {
s7Connector = new Sharp7Connector(plcConnectionSettings, varaibleNameParser); s7Connector = new Sharp7Connector(plcConnectionSettings, varaibleNameParser){Logger = Logger};
ConnectionState = s7Connector.ConnectionState; ConnectionState = s7Connector.ConnectionState;
await s7Connector.InitializeAsync(); await s7Connector.InitializeAsync();
@@ -66,7 +67,7 @@ namespace Sharp7.Rx
} }
catch (Exception e) catch (Exception e)
{ {
s7Connector.Logger.LogError(e, "Error while connecting to PLC"); Logger?.LogError(e, "Error while connecting to PLC");
} }
}); });
#pragma warning restore 4014 #pragma warning restore 4014
@@ -387,7 +388,7 @@ namespace Sharp7.Rx
.Select(states => states == Enums.ConnectionState.Connected) .Select(states => states == Enums.ConnectionState.Connected)
.SelectMany(connected => GetAllValues(connected, connector)) .SelectMany(connected => GetAllValues(connected, connector))
.RepeatAfterDelay(cycle) .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) .TakeUntil(disposingSubject)
.Subscribe(); .Subscribe();
} }
@@ -430,7 +431,7 @@ namespace Sharp7.Rx
var min = performanceCoutner.Min(); var min = performanceCoutner.Min();
var max = performanceCoutner.Max(); 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); MultiVarRequestMaxItems);
performanceCoutner.Clear(); performanceCoutner.Clear();
} }