Logger

Output Configuration

WithRootDirectory sets the base directory for log files. WithFilename sets a static filename. WithFilenamePattern appends a timestamp using the FilenamePattern enum, so the logger rotates into a new file as time passes. WithDirectoryStructure organizes files into date-based folders under the root using the DirectoryStructure enum, creating them as they are needed. The file extension does not decide the format; a logger writes lines whatever the file is called. A pattern and a structure both move the file, so read LogDirectory, LogFilename or LogFullPath afterwards when something else needs to find it. Published events reach four destinations, each with its own switch: WithPublishToConsole for the console, WithPublishToDebug for the debug output, WithPublishToControl for a GPALTextArea on a form, and a handler you attach yourself. There are two handlers, and the event chooses between them rather than a setting. An event that carries an exception, or is typed EXCEPTION, goes to the exception handler. Everything else goes to the information handler. Each destination then has a mask naming the event types it will take, and all of them default to All. WithExceptionHandlerEvents masks the exception handler, WithInformationHandlerEvents the information handler, and WithHandlerEvents sets those two at once. WithControlEvents masks the control. A mask only narrows what a destination already receives, so masking the exception handler cannot send its events somewhere else.

Examples

GPAL Fluent: High-level fluent C# API

//WithFilenamePattern and WithFilename work together: the pattern decorates the name rather than replacing it. WithDirectoryStructure(DirectoryStructure.YMD) creates year, month, and day subdirectories under the root. The three read-only members are the reliable answer to where the file went, and they are what you pass to WithAttachment when the run mails its own log.

// A daily rotating file in date-based folders

IGPALLogger daily = GPAL.Logger

.WithRootDirectory(@"C:AppLogs")

.WithFilenamePattern(FilenamePattern.YMD)

.WithDirectoryStructure(DirectoryStructure.YMD)

.WithFilename("report.log")

.ToGPALObject();


GPAL.WithPublishToLogger(daily);

GPAL.PublishSimpleEvent(GPALEventType.NOTICE, "Daily report generated");


// C:AppLogs2026821 eport_20260821.log

GPAL.PublishSimpleEvent(GPALEventType.NOTICE, $"[{daily.LogDirectory}]"); // C:AppLogs2026821

GPAL.PublishSimpleEvent(GPALEventType.NOTICE, $"[{daily.LogFilename}]"); // report_20260821.log

GPAL.PublishSimpleEvent(GPALEventType.NOTICE, $"[{daily.LogFullPath}]"); // both, joined


// A fixed file that never moves

IGPALLogger fixedFile = GPAL.Logger

.WithRootDirectory(@"C:AppLogs")

.WithFilename("debug.log")

.ToGPALObject();


fixedFile.WithLogEventTypes(GPALEventType.DEBUG | GPALEventType.DEEPDEBUG);

GPAL.WithPublishToLogger(fixedFile);

💬 Ask GPAL