Skip to content

Commit

Permalink
[NEW-FEATURE] Add logging for the CRUD operations including time meas…
Browse files Browse the repository at this point in the history
…urement. (#473)

* Create draft PR for #472

* Add identity management to AxoApplication and AxoLogger

- Added `System.Security.Principal` namespace to `AxoApplication.cs` and `IAxoApplication.cs`.
- Introduced `ControllerIdentity` property in `AxoApplication` and `IAxoApplication`.
- Created new internal class `ControllerIdentity` inheriting from `GenericIdentity`.
- Updated `AxoLogger` to use `ControllerIdentity` from `AxoApplication.Current`.

Add ControllerIdentity property and refactor logging

Included System.Security.Principal namespace in AxoApplication.cs and IAxoApplication.cs. Added ControllerIdentity property to AxoApplication class and IAxoApplication interface. Introduced ControllerIdentity class inheriting from GenericIdentity in ControllerIdentity.cs. Updated AxoLogger.cs to use ControllerIdentity from AxoApplication.Current for logging.

* Add Stopwatch to log execution time of remote operations

Introduce Stopwatch to AxoDataExchange class to measure execution time.
Add private Stopwatch field `sw` to the class.
Modify RemoteCreate, RemoteRead, RemoteUpdate, RemoteDelete,
RemoteEntityExist, and RemoteCreateOrUpdate methods to start and stop
the stopwatch. Log the elapsed time with record identifier, data
exchange symbol, and time in milliseconds using AxoApplication.Current.Logger.Information.

---------

Co-authored-by: PTKu <61538034+PTKu@users.noreply.github.com>
  • Loading branch information
IX-BOT and PTKu authored Dec 10, 2024
1 parent 6cea73d commit 51752e3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/base/src/AXOpen.Base.Abstractions/App/AxoApplication.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AXOpen.Logging;
using System.Security.Principal;
using AXOpen.Logging;

namespace AXOpen
{
Expand Down Expand Up @@ -39,5 +40,7 @@ public IAxoApplication Build()
/// Get currently running application.
/// </summary>
public static IAxoApplication Current => _current;

public IIdentity ControllerIdentity { get; } = new ControllerIdentity();
}
}
6 changes: 6 additions & 0 deletions src/base/src/AXOpen.Base.Abstractions/App/IAxoApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// https://github.com/ix-ax/axsharp/blob/dev/LICENSE
// Third party licenses: https://github.com/ix-ax/axsharp/blob/dev/notices.md

using System.Security.Principal;
using AXOpen.Logging;

namespace AXOpen;
Expand All @@ -18,4 +19,9 @@ public interface IAxoApplication
/// Gets logger configured for this application.
/// </summary>
ILogger Logger { get; }

/// <summary>
/// Provides identity for the logging operation for controller provenience.
/// </summary>
IIdentity ControllerIdentity { get; }
}
8 changes: 8 additions & 0 deletions src/base/src/AXOpen.Base.Abstractions/ControllerIdentity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Security.Principal;

namespace AXOpen;

/// <summary>
/// Provides identity for the operations from the controller.
/// </summary>
internal class ControllerIdentity() : GenericIdentity("Controller");
13 changes: 7 additions & 6 deletions src/core/src/AXOpen.Core/AxoLogger/AxoLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,26 @@ await Task.Run(async () =>

private void CreateLogEntry(eLogLevel level, string message, ITwinObject? sender)
{
var controllerIdentity = AxoApplication.Current.ControllerIdentity;
switch (level)
{
case eLogLevel.Verbose:
_logger.Verbose($"{message}", sender, new GenericIdentity("Controller"), sender);
_logger.Verbose($"{message}", sender, controllerIdentity, sender);
break;
case eLogLevel.Debug:
_logger.Debug($"{message}", sender, new GenericIdentity("Controller"), sender);
_logger.Debug($"{message}", sender, controllerIdentity, sender);
break;
case eLogLevel.Information:
_logger.Information($"{message}", sender, new GenericIdentity("Controller"), sender);
_logger.Information($"{message}", sender, controllerIdentity, sender);
break;
case eLogLevel.Warning:
_logger.Warning($"{message}", sender, new GenericIdentity("Controller"), sender);
_logger.Warning($"{message}", sender, controllerIdentity, sender);
break;
case eLogLevel.Error:
_logger.Error($"{message}", sender, new GenericIdentity("Controller"), sender);
_logger.Error($"{message}", sender, controllerIdentity, sender);
break;
case eLogLevel.Fatal:
_logger.Fatal($"{message}", sender, new GenericIdentity("Controller"), sender);
_logger.Fatal($"{message}", sender, controllerIdentity, sender);
break;
}
}
Expand Down
31 changes: 26 additions & 5 deletions src/data/src/AXOpen.Data/DataExchange/AxoDataExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Third party licenses: https://github.com/ix-ax/axsharp/blob/dev/notices.md

using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Compression;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -181,16 +182,18 @@ public IEnumerable<IBrowsableDataObject> GetRecords(string identifier)
return DataRepository.GetRecords(identifier).Cast<IBrowsableDataObject>();
}

private Stopwatch sw = new Stopwatch();

/// <inheritdoc />
public async Task<bool> RemoteCreate(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);

var cloned = await ((ITwinObject)DataEntity).OnlineToPlain<TPlain>();

var cloned = await ((ITwinObject)DataEntity).OnlineToPlain<TPlain>();
Repository.Create(identifier, cloned);

sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' created in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}

Expand All @@ -199,9 +202,12 @@ public async Task<bool> RemoteRead(string identifier)
{
try
{
sw.Restart();
await Operation.ReadAsync();
var record = Repository.Read(identifier);
await ((ITwinObject)DataEntity).PlainToOnline(record);
sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' read from '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}
catch (Exception exception)
Expand All @@ -213,36 +219,47 @@ public async Task<bool> RemoteRead(string identifier)
/// <inheritdoc />
public async Task<bool> RemoteUpdate(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);

var cloned = await ((ITwinObject)DataEntity).OnlineToPlain<TPlain>();

cloned.Hash = HashHelper.CreateHash(cloned);
Repository.Update(identifier, cloned);
sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' updated in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}

/// <inheritdoc />
public async Task<bool> RemoteDelete(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);
Repository.Delete(identifier);
sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' deleted in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}

/// <inheritdoc />
public async Task<bool> RemoteEntityExist(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);
return Repository.Exists(identifier);
var retVal = Repository.Exists(identifier);
sw.Stop();
AxoApplication.Current.Logger.Information($"Information about record '{identifier}' existence in '{this.Symbol}' retrieved in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return retVal;
}

/// <inheritdoc />
public async Task<bool> RemoteCreateOrUpdate(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);

Expand All @@ -258,6 +275,10 @@ public async Task<bool> RemoteCreateOrUpdate(string identifier)
{
Repository.Create(identifier, cloned);
}

sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' created in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms' using `Create or update` function.", this, AxoApplication.Current.ControllerIdentity);

return true;
}

Expand Down

0 comments on commit 51752e3

Please sign in to comment.