Logging to Windows logs

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.

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

Pieter Scheldeman2016-11-21 16:49:45

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.

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;

Pieter Scheldeman2016-11-22 09:45:51

I see. Great idea. I'll try that as well. Thank you for your quick response!