Web forum is in read-only mode. Login as active registered customer for write access
  Forum Search   New Posts New Posts

Logging to Windows logs

 Post Reply Post Reply
Author
Ronald Janse View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 Aug 2015
Posts: 267
Post Options Post Options   Quote Ronald Janse Quote  Post ReplyReply Direct Link To This Post Topic: Logging to Windows logs
    Posted: 21 Nov 2016 at 3:48pm
First question:
Is it possible to write some more information to the Windows Logs, using TMSLogging?
At the moment all I can write to it is a line of text. I would like to be able to write the Event ID to the logs, especially when an Error occurs.

Second question:
Is it possible to write less of the 'standard' information to the Windows logs as to a standard log? I don't need al the initialisationtexts etc. to appear in the Windows log.
Back to Top
Pieter Scheldeman View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 3331
Post Options Post Options   Quote Pieter Scheldeman Quote  Post ReplyReply Direct Link To This Post Posted: 21 Nov 2016 at 4:45pm
Hi, 

Currently it's not possible to log the EventCategory and EventID. We haved added this on our feature request list. It's actually very simple to support custom logging by assigning an event handler to the OnOutput event.

uses
  FMX.TMSLogging, TMSLoggingUtils;

procedure TForm1.DoLoggerOutput(Sender: TObject;
  AOutputInformation: TTMSLoggerOutputInformation);
var
  Msg, Server, Source: string;
  EventLog: integer;
  P: Pointer;
  EventType: Word;
  EventCategory: Word;
  EventID: Word;
begin
  msg := TTMSLoggerUtils.StripHTML(TTMSLoggerUtils.GetConcatenatedLogMessage(AOutputInformation));
  P := PWideChar(msg);

  Server := 'MyServer';
  Source := 'MyApplication';

  if Server = '' then
    EventLog := RegisterEventSource(nil, PWideChar(Source))
  else
    EventLog := RegisterEventSource(PWideChar(Server), PWideChar(Source));

  case AOutputInformation.LogLevel of
    TTMSLoggerLogLevel.Warning: EventType := EVENTLOG_WARNING_TYPE;
    TTMSLoggerLogLevel.Error: EventType := EVENTLOG_ERROR_TYPE;
    else
      EventType := EVENTLOG_INFORMATION_TYPE;
  end;

  EventCategory := 0;
  EventID := 0;

  if EventLog <> 0 then
  try
    ReportEvent(EventLog,
          EventType,
          EventCategory,
          EventId,
          nil,
          1,
          0,
          @P,
          nil);
  finally
    DeregisterEventSource(EventLog);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSLogger.OnOutput := DoLoggerOutput;
end;

For your second question, you can select which values are send to the output handler by setting the Outputs property.

for example:

TMSLogger.Outputs := [loTimeStamp, loValue];

will only output the timestamp and value.

Formatting of these values are controlled under TMSLogger.OutputFormats


Edited by Pieter Scheldeman - 21 Nov 2016 at 4:49pm
Back to Top
Ronald Janse View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 Aug 2015
Posts: 267
Post Options Post Options   Quote Ronald Janse Quote  Post ReplyReply Direct Link To This Post Posted: 22 Nov 2016 at 9:38am
Thank you for your answer on question 1. I will try that.

Concerning question 2: I think I did not describe the problem well. Wat I want to achieve is that some logging (especially the Info statements) DO go to - say - a text logfile, but DO NOT go to the Windows Logging. I want to use the windows logging only for important things, like Warnings and Errors.
Back to Top
Pieter Scheldeman View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 3331
Post Options Post Options   Quote Pieter Scheldeman Quote  Post ReplyReply Direct Link To This Post Posted: 22 Nov 2016 at 9:45am
Hi, 

The logging statements are sent to each assigned output handler, regardless of the level.
There is no option to choose which levels are logged by the output handler.

In order to manage this, you'll need to create 2 loggers. One for handling warnings/errors and one for all the other logging. 

var
  logger1, logger2: TTMSFMXLogger;
begin
  logger1 := TTMSFMXLogger.Create;
  logger2 := TTMSFMXLogger.Create;

  logger1.RegisterOutputHandlerClass(TTMSLoggerEventLogOutputHandler, [...]);
  logger2.RegisterOutputHandler(TTMSLoggerTextOutputHandler, [...]);

  logger1.Warning(...);
  logger2.Info(...);
end;


Edited by Pieter Scheldeman - 22 Nov 2016 at 9:45am
Back to Top
Ronald Janse View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 Aug 2015
Posts: 267
Post Options Post Options   Quote Ronald Janse Quote  Post ReplyReply Direct Link To This Post Posted: 22 Nov 2016 at 9:47am
I see. Great idea. I'll try that as well. Thank you for your quick response!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down