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

Use syslog format for logging, add service request scopes to logs #314

Merged
merged 10 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 0 additions & 7 deletions .editorconfig

This file was deleted.

319 changes: 319 additions & 0 deletions src/.editorconfig

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/AlarmCondition/Model/AreaState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;
using System.Collections.Generic;
using Opc.Ua;

namespace AlarmCondition
Expand Down
3 changes: 0 additions & 3 deletions src/AlarmCondition/Model/ModelUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;
using System.Collections.Generic;
using System.Text;
using Opc.Ua;
using Opc.Ua.Server;

namespace AlarmCondition
{
Expand Down
3 changes: 0 additions & 3 deletions src/AlarmCondition/Model/Namespaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;
using System.Collections.Generic;

namespace AlarmCondition
{
/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/AlarmCondition/NodeHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;
using System.Text;
using Opc.Ua;
using Opc.Ua.Server;

namespace AlarmCondition
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
* ======================================================================*/

using System;
using System.Collections.Generic;
using Opc.Ua;

namespace AlarmCondition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
* ======================================================================*/

using System;
using System.Collections.Generic;

namespace AlarmCondition
{
Expand Down
13 changes: 7 additions & 6 deletions src/CliOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace OpcPlc;
namespace OpcPlc;

using Microsoft.Extensions.Logging;
using Mono.Options;
using Opc.Ua;
using OpcPlc.Helpers;
Expand Down Expand Up @@ -31,11 +32,11 @@ public static Mono.Options.OptionSet InitCommandLineOptions()
}
}
},
{ "ll|loglevel=", "the loglevel to use (allowed: fatal, error, warn, info, debug, verbose).\nDefault: info", (string s) => {
var logLevels = new List<string> {"fatal", "error", "warn", "info", "debug", "verbose"};
{ "ll|loglevel=", "the loglevel to use (allowed: critical, warn, info, debug, trace Default: info", (string s) => {
var logLevels = new List<string> {"critical", "error", "warn", "info", "debug", "trace"};
Copy link
Collaborator

@luiscantero luiscantero Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls add \n just before default to be consistent with the rest below #Closed

if (logLevels.Contains(s.ToLowerInvariant()))
{
Program.LogLevel = s.ToLowerInvariant();
Program.LogLevelCli = s.ToLowerInvariant();
}
else
{
Expand Down Expand Up @@ -232,7 +233,7 @@ public static void PrintUsage(Mono.Options.OptionSet options)
using var stringWriter = new StringWriter(sb);
options.WriteOptionDescriptions(stringWriter);

Program.Logger.Information(sb.ToString());
Program.Logger.LogInformation(sb.ToString());
}

/// <summary>
Expand All @@ -256,7 +257,7 @@ private static List<string> ParseListOfStrings(string list)
else if (list.Contains(','))
{
strings = list.Split(',').ToList();
strings.ForEach(st => st.Trim());
strings.ForEach(st => st = st.Trim());
strings = strings.Select(st => st.Trim()).ToList();
}
else
Expand Down
46 changes: 23 additions & 23 deletions src/DeterministicAlarms/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
namespace OpcPlc.DeterministicAlarms.Configuration;
namespace OpcPlc.DeterministicAlarms.Configuration;

using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

public class Configuration
{
{
static readonly JsonSerializerOptions _fromJsonOptions = new JsonSerializerOptions {
ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter(),
},
};

static readonly JsonSerializerOptions _toJsonOptions = new JsonSerializerOptions {
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter()
}
};

public List<Folder> Folders { get; set; }

public Script Script { get; set; }

public string ToJson()
{
return JsonSerializer.Serialize(this,
new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter()
}
});
return JsonSerializer.Serialize(this, _toJsonOptions);
}

public static Configuration FromJson(string json)
{
return JsonSerializer.Deserialize<Configuration>(json,
new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter(),
},
});
return JsonSerializer.Deserialize<Configuration>(json, _fromJsonOptions);
}
}
15 changes: 8 additions & 7 deletions src/DeterministicAlarms/DeterministicAlarmsNodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.IO;
using static OpcPlc.Program;
using Microsoft.Extensions.Logging;

public class DeterministicAlarmsNodeManager : CustomNodeManager2
{
Expand Down Expand Up @@ -49,7 +50,7 @@ public DeterministicAlarmsNodeManager(IServerInternal server, ApplicationConfigu
}
catch (Exception ex)
{
Logger.Error(ex, "Cannot read or decode deterministic alarm script file");
Logger.LogError(ex, "Cannot read or decode deterministic alarm script file");
}
}

Expand Down Expand Up @@ -115,12 +116,12 @@ private void ReplayScriptStart(Configuration.Configuration scriptConfiguration)
try
{
VerifyScriptConfiguration(scriptConfiguration);
Logger.Information("Script starts executing");
Logger.LogInformation("Script starts executing");
var scriptEngine = new ScriptEngine(scriptConfiguration.Script, OnScriptStepAvailable, _timeService);
}
catch (ScriptException ex)
{
Logger.Error($"Script Engine Exception '{ex.Message}'\nSCRIPT WILL NOT START");
Logger.LogError($"Script Engine Exception '{ex.Message}'\nSCRIPT WILL NOT START");
throw;
}
}
Expand All @@ -134,7 +135,7 @@ private void OnScriptStepAvailable(Step step, long loopNumber)
{
if (step == null)
{
Logger.Information("SCRIPT ENDED");
Logger.LogInformation("SCRIPT ENDED");
}
else
{
Expand Down Expand Up @@ -199,16 +200,16 @@ private void PrintScriptStep(Step step, long loopNumber)
{
if (step.Event != null)
{
Logger.Information($"({loopNumber}) -\t{step.Event.AlarmId}\t{step.Event.Reason}");
Logger.LogInformation($"({loopNumber}) -\t{step.Event.AlarmId}\t{step.Event.Reason}");
foreach (var sc in step.Event.StateChanges)
{
Logger.Information($"\t\t{sc.StateType} - {sc.State}");
Logger.LogInformation($"\t\t{sc.StateType} - {sc.State}");
}
}

if (step.SleepInSeconds > 0)
{
Logger.Information($"({loopNumber}) -\tSleep: {step.SleepInSeconds}");
Logger.LogInformation($"({loopNumber}) -\tSleep: {step.SleepInSeconds}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/PnJsonHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace OpcPlc.Helpers;

using Microsoft.Extensions.Logging;
using OpcPlc.PluginNodes.Models;
using Serilog;
using System;
using System.Collections.Immutable;
using System.IO;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static async Task PrintPublisherConfigJsonAsync(string pnJsonFileName, st
sb.AppendLine("]");

string pnJson = sb.ToString();
logger.Information("OPC Publisher configuration file: {pnJsonFile}", $"{pnJsonFileName}{pnJson}");
logger.LogInformation("OPC Publisher configuration file: {pnJsonFile}", $"{pnJsonFileName}{pnJson}");

await File.WriteAllTextAsync(pnJsonFileName, pnJson.Trim()).ConfigureAwait(false);
}
Expand Down
32 changes: 32 additions & 0 deletions src/Logging/LoggingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

namespace OpcPlc.Logging;

using Microsoft.Extensions.Logging;
using System;

/// <summary>
/// Provides utility for creating logger factory.
/// </summary>
public static class LoggingProvider
{
/// <summary>
/// Create ILoggerFactory object with default configuration.
/// </summary>
public static ILoggerFactory CreateDefaultLoggerFactory(LogLevel level)
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(options => options.FormatterName = nameof(SyslogFormatter))
.SetMinimumLevel(level)
.AddConsoleFormatter<
SyslogFormatter,
SyslogFormatterOptions>();
});

return loggerFactory;
}
}
Loading
Loading