Tutorials

Tutorials

Logging to a Database and Sending Email with GPAL

GPAL.Logger can write log entries to a SQL database instead of a file, accepting plain strings, settings objects, and whole grids. GPAL.Mail sends email through a configured SMTP server with To, CC, and BCC recipients. Together they show how GPAL subsystems share the same fluent factory pattern across very different jobs.

Complete Program

The whole workflow: define a database target for logging, build a logger that writes to it, log a few different kinds of data, then configure and send an email.

using GenerallyPositive;

using static GenerallyPositive.Enums;

GPALDatabase database = (GPALDatabase)GPAL.Database

.WithServerName(@"MYSERVERSQLEXPRESS")

.WithDatabaseName("GPAL")

.WithDatabaseType(DatabaseType.SQLServer)

.WithCreateAs("Insert Into TestInput (SearchTerm) Values (@SearchTerm)")

.WithNVarCharColumn("SearchTerm");

IGPALGrid<string> grid = GPAL.Grid.ToGPALObject();

GPAL.WithPublishToDebug().WithAutoUpdateWebDriver();

IGPALLogger gPALLogger = GPAL.Logger.WithDatabase(database).ToGPALObject();

gPALLogger.Log(GPAL.GPALSettings);

gPALLogger.Log("Log This");

gPALLogger.WithDelimiter(',').Log("AND, THIS");

GPAL.Converter.WithInput(GPAL.GPALSettings).SaveTo(ref grid);

gPALLogger.Log(grid);

IGPALMail gPALMail = GPAL.Mail

.WithSMTPServerName("smtp.example.com")

.WithSMTPPortNum(587)

.WithFromEmailAddress("reports@example.com")

.WithCcEmailAddress("cc@example.com").WithName("CC")

.WithBccEmailAddress("bcc@example.com").WithName("BCC")

.WithToEmailAddress("recipient@example.com").WithName("Recipient")

.WithSubject("Run Complete").ToGPALObject();

gPALMail.Send();

Define a Database Target

GPAL.Database describes a SQL connection and, optionally, an insert statement for new rows. WithCreateAs gives the parameterized SQL, and WithNVarCharColumn declares the column type for each parameter. This same GPALDatabase object can be reused anywhere GPAL needs database access - here, as a logging destination.

GPALDatabase database = (GPALDatabase)GPAL.Database

.WithServerName(@"MYSERVERSQLEXPRESS")

.WithDatabaseName("GPAL")

.WithDatabaseType(DatabaseType.SQLServer)

.WithCreateAs("Insert Into TestInput (SearchTerm) Values (@SearchTerm)")

.WithNVarCharColumn("SearchTerm");

Log Settings, Strings, and Grids

WithDatabase points the logger at the database object instead of a file, and ToGPALObject() finalizes it into an IGPALLogger. From there, Log accepts plain strings, the live GPAL.GPALSettings object, or an IGPALGrid - GPAL serializes whatever you hand it into the configured log destination. WithDelimiter changes how multi-part log entries are joined.

IGPALLogger gPALLogger = GPAL.Logger.WithDatabase(database).ToGPALObject();

gPALLogger.Log(GPAL.GPALSettings);

gPALLogger.Log("Log This");

gPALLogger.WithDelimiter(',').Log("AND, THIS");

TIP

Logging GPAL.GPALSettings captures the full configuration GPAL was running with at that moment - handy for reproducing an issue later, since you know exactly what driver locations, automation engines, and flags were in effect.

Convert Settings Into a Grid Before Logging

GPAL.Converter.WithInput().SaveTo() turns a settings object (or any supported source) into an IGPALGrid by reference. Once it is a grid, the same logger that accepted a string or a settings object can log it as tabular data.

IGPALGrid<string> grid = GPAL.Grid.ToGPALObject();

GPAL.Converter.WithInput(GPAL.GPALSettings).SaveTo(ref grid);

gPALLogger.Log(grid);

Send an Email with To, CC, and BCC

GPAL.Mail is configured the same way as every other subsystem: With* methods set the SMTP server, port, sender, and recipients, then ToGPALObject() returns an IGPALMail ready to send. WithName attaches a display name to whichever address was most recently set with WithCcEmailAddress, WithBccEmailAddress, or WithToEmailAddress.

IGPALMail gPALMail = GPAL.Mail

.WithSMTPServerName("smtp.example.com")

.WithSMTPPortNum(587)

.WithFromEmailAddress("reports@example.com")

.WithCcEmailAddress("cc@example.com").WithName("CC")

.WithBccEmailAddress("bcc@example.com").WithName("BCC")

.WithToEmailAddress("recipient@example.com").WithName("Recipient")

.WithSubject("Run Complete").ToGPALObject();

gPALMail.Send();

WARNING

Each WithName call attaches a display name to whichever WithToEmailAddress, WithCcEmailAddress, or WithBccEmailAddress call came immediately before it. Order matters - if you set multiple recipients, call WithName right after each one.