Skip to content

Commit

Permalink
Merge branch '466-_NEW-FEATURE_log_button_action_in_DataExchange_Edit…
Browse files Browse the repository at this point in the history
…_window' of https://github.com/ix-ax/AXOpen into 466-_NEW-FEATURE_log_button_action_in_DataExchange_Edit_window
  • Loading branch information
blazej.kuhajda committed Dec 11, 2024
2 parents 3b0a2b2 + 488d5cc commit 038d2eb
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 038d2eb

Please sign in to comment.