Jump to content

Recommended Posts

I'm fairly new to RAD Server and while I have a small module working, pulling data from a database, I'd like to be able to do some logging. Sure, I can create another endpoint and dump stuff in a JSON result, but that's not really behind-the-scenes logging. And yes, I could create a log table and insert records in there.

 

But there's an intriguing class in the EMS namespace called TEMSLoggingService. I can't find any documentation or examples that explain what this is for or how to use it.

 

Anyone have any wisdom on this to share?

Share this post


Link to post

I looked in DocWiki but could not find it. This is a start, albeit not much more than viewing the source.

 

Thanks for the link!

Share this post


Link to post
unit MyUnit;

interface

{$IFDEF RADSERVER_PACKAGE}
uses .....
     EMS.ResourceAPI;

type
  THSLoggerRADServerDebugTargetQueue = class(THSLoggerBaseTargetQueue)
  protected
    function HandleTargetItem(const AItem: THSLLoggerItem): Boolean; override;
  public
  end;
{$ENDIF}

implementation

{$IFDEF RADSERVER_PACKAGE}

{ THSLoggerRADServerDebugTargetQueue }

function THSLoggerRADServerDebugTargetQueue.HandleTargetItem(const AItem: THSLLoggerItem): Boolean;
var
  line: string;
begin
  inherited;
  .....
    TEMSEndpointEnvironment.Instance.LogMessage(line);
  ....
end;

{$ENDIF}

end.

 

Edited by mvanrijnen
added code
  • Like 1

Share this post


Link to post

Somehow inserting code goes wrong sometime, my text of my post disappeared.

You just need the EMS.ResourceAPI in your uses and then you can use the TEMSEndPointEnvironment.Instance.LogMessage('your log text');

 

In debugmode (running your RADServer package from the IDE), the logging goes into the radserver console window, where the radserver itself also logs. 

We are not in production yet, so i do not know at the moment where the logging goes in a production environment 🙂

 

 

I use this in a logging framework, which also can log to file, database etc etc.

 

 

Did not see the TEMSLoggingService before, have to inspect that, maybe it's the better way to work.

 

 

[edit, added first code for the emsloggingservice)

so, first tryout for the loggingservice:

 

unit .......;

interface

uses .....
     EMS.Services;

type
  THSLoggerRADServerDebugTargetQueue = class(THSLoggerBaseTargetQueue)
  private
    femslogsvc : IEMSLoggingService;
  protected
    function HandleTargetItem(const AItem: THSLLoggerItem): Boolean; override;
  public
    procedure DoBeforeStart; override;
    procedure DoAfterStop; override;

    {TODO -oOwner -cGeneral : CheckCanStart wordt helemaal niet aangeroepen ???? }
    procedure CheckCanStart(var ACanStart: Boolean; var AMessage: string); override;
  end;

implementation

uses .....,
     EMS.ResourceAPI;

{ THSLoggerRADServerDebugTargetQueue }

procedure THSLoggerRADServerDebugTargetQueue.CheckCanStart(var ACanStart: Boolean; var AMessage: string);
begin
  inherited;
  ACanStart := True;
end;

procedure THSLoggerRADServerDebugTargetQueue.DoAfterStop;
begin
  femslogsvc := nil;
  inherited;
end;

procedure THSLoggerRADServerDebugTargetQueue.DoBeforeStart;
begin
  femslogsvc := nil;
  inherited;

  if TEMSServices.TryGetService<IEMSLoggingService>(femslogsvc) then
  begin
    if not femslogsvc.LoggingEnabled then
    begin
      femslogsvc := nil;
    end;
  end;
end;

function THSLoggerRADServerDebugTargetQueue.HandleTargetItem(const AItem: THSLLoggerItem): Boolean;
var
 obj : TJSONObject;
begin
  inherited;
  Result := False;
  try
    if Assigned(femslogsvc) then
    begin
      obj := TJSONObject.Create();
      obj.AddPair('user', AItem.UserName);
      obj.AddPair('computer', AItem.ComputerName);
      obj.AddPair('datetime', DateToISO8601(AItem.DateTime));
      obj.AddPair('message', AItem.LogMessage);
      obj.AddPair('position', AItem.Position);
      femslogsvc.Log(AItem.LogType.AsString, obj);
    end;
    Result := True;
  except
    on e: exception do
    begin
      // DebugLog('xx', 'xxxx');
    end;
  end;
end;

end.

 

Edited by mvanrijnen

Share this post


Link to post
3 hours ago, mvanrijnen said:

TEMSEndpointEnvironment.Instance.LogMessage(line);

This is perfect--exactly what I was looking for!  Thank you!

 

I'll have to spend some time checking out your THSLoggerRADServerDebugTargetQueue class. That looks like it could useful as well.

 

Thanks again!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×