Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to log to "Applications and Services Logs/MyApp"



Permlink Replies: 1 - Last Post: Oct 18, 2017 10:46 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Robert Small

Posts: 44
Registered: 5/12/98
How to log to "Applications and Services Logs/MyApp"
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2017 8:28 PM
I have an old service app originally written about 10 years ago in
D2006 and recompiled in D2009, XE4 and now 10.1 Berlin

It is working OK, but the logging messages are all being written to
"Windows Logs/Application" and lumped in with a miriad other log
messages.

What I would like to do is to have them logged to "Applications and
Services Logs/MyApp" so that they are all in their own folder.

I've had a look at some of the Microsoft documentation, but have to
admit that I am finding it very hard to digest.

Can anyone help with a "simple" Delphi example?

Thanks
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to log to "Applications and Services Logs/MyApp"
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 18, 2017 10:46 AM   in response to: Robert Small in response to: Robert Small
Robert Small wrote:

What I would like to do is to have them logged to "Applications and
Services Logs/MyApp" so that they are all in their own folder.

There are two different ways to accomplish this, depending on which
logging API you want to use:

Event Logging
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363652.aspx

Windows Event Log
https://msdn.microsoft.com/en-us/library/windows/desktop/aa385780.aspx

I use the first (older) API (because I wrote my service way back in the
Win9x days). That API has been deprecated since Vista, but it still
works, and is much easier and more flexible to work with than the newer
API (which involves manifests, schemas, event providers, etc).

In the service's AfterInstall event, create the following Registry key:

HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\<My Log Display Name>\

For each "App Name" that you want to go to your log, create the
following Registry key:

HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\<My Log Display
Name>\<App Name>\

Where "App Name" is the value you pass to RegisterEventSource() when
logging strings with ReportEvent() at runtime.

On XP and earlier only, add the following values to the "My Log" key:

(SZ) "PrimaryModule" = "Your primary App Name"
(MULTI_SZ) "Sources" = "list of App Names that go to this log"

For your "primary" app name, and for each additional app name (Vista+
only), add the following values to the "App Name" key:

(SZ) "EventMessageFile" = "full path to file containing message
resource strings"
(SZ) "CategoryMessageFile" = "full path to file containing category
resource strings"
(DWORD) "CategoryCount" = # of categories in file
(DWORD) "TypesSupported" = bitmask of catgories in file

See MSDN for more details:

Event Sources
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363661.aspx

Microsoft prefers people to compile log messages into resource files
using Microsoft's MC.EXE compiler:

Message Files
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363669.aspx

Message Compiler
https://msdn.microsoft.com/en-us/library/windows/desktop/aa385638.aspx

But, that only comes with Visual Studio. If you don't have it, you
have to create the message resources manually. You can use any
resource compiler/editor for that (including Delphi's). The trick is
to define a MESSAGETABLE resource with ID 1, containing the desired
resource strings, where:

- Category strings are identified by ID number, starting at 1
consecutively:

Event Categories
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363649.aspx

- Message strings are identified by an event ID number that consists of
severity, customer flag, facility, and status code:

Event Identifiers
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363651.aspx

I also include a resource string with ID 0 and value "%s", so apps can
pass pre-formatted strings to ReportEvent() and be logged as-is.

When logging an event at runtime, you would first call
RegisterEventSource() to open the log with the desired App Name, then
call ReportEvent() with the desired category and event IDs, and
optional message parameters.

In my case, I compile my resource strings into a single .res file and
link it into my service .EXE, so both "EventMessageFile" and
"CategoryMessageFile" Registry values are set to the string returned by
calling ParamStr(0). But you can also use other executables, such as
resource DLLs.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02