Logger

Database Logging

WithDatabase points a logger at a table instead of a file. Configure the GPALDatabase first with GPAL.Database, giving it a connection and a parameterized insert through WithCreateAs plus the parameter that receives the entry, then hand it to the logger. ToGPALObject returns the IGPALLogger, which is attached with GPAL.WithPublishToLogger like any other. Each entry becomes one row, and the value written carries the event type in front of the message, because the database supplies its own timestamp but cannot know what kind of event it was. A write that fails does not throw and does not stop the run. It lands on the database as LastError.

NOTE

A failed insert answers like a successful one, so a logger cannot tell by return value alone. GPALDatabase.LastError carries the server error number, the SQL that ran, the row index being written, and that row's values. Read it after a phase of work rather than wrapping each publish in a try.

Examples

GPAL Fluent: High-level fluent C# API

//A database logger and a file logger are the same kind of object attached the same way, so a run can have both: one file beside the exe for the operator, one table for everything that ever ran. Each subscribes to its own event types through WithLogEventTypes.

// The insert the logger writes each entry through

GPALDatabase logDb = (GPALDatabase)GPAL.Database

.WithConnectionString(connStr)

.WithCreateAs("INSERT INTO AppLog (Entry) VALUES (@Entry)")

.WithNVarCharParameter("Entry");


IGPALLogger toDatabase = GPAL.Logger.WithDatabase(logDb).ToGPALObject();


toDatabase.WithLogEventTypes(GPALEventType.USERALL);

GPAL.WithPublishToLogger(toDatabase);


GPAL.PublishSimpleEvent(GPALEventType.CAUTION, "User action recorded");

// Row written: CAUTION | User action recorded


// Nothing threw. Ask whether it landed

if (null != logDb.LastError)

GPAL.PublishSimpleEvent(GPALEventType.NOTICE, $"[{logDb.LastError.Number}] [{logDb.LastError.Sql}]");

💬 Ask GPAL