Custom Sinks (Filters)
C1Logger supports extensibility through custom log sinks.
A log sink allows you to send log messages to any destination (e.g., console, file, database, cloud logging, etc.).
Implementing a Custom Sink
To create your own sink, implement the C1Log.ILogSink
interface.
The interface supports asynchronous logging, log level filtering, and resource cleanup.
Registering Your Sink
Add your sink before logging:
Example Usage
Filtering for Specific Log Levels in a Custom Sink
By default, a custom sink in C1Logger uses the MinLogLevel property to determine which log messages it receives. However, this property only allows you to filter for all log levels at or above a certain severity (e.g., all logs that are Critical or less severe).
If you want your sink to process only specific log levels (for example, only Informational and Critical), you can implement the filtering logic inside your sink’s WriteAsync method. This gives you full control over which messages are handled.
You can also use this to filter for every log below or above a log level.
Example: Sink for Only Informational and Critical Logs
Sending Logs to Multiple Sinks
C1Logger supports registering multiple custom sinks. When you add more than one sink using C1Log.AddSink
, every log message will be sent to all registered sinks that match the log level filter.
This is useful if you want to, for example, write logs to a file, display them in the console, and send them to a cloud logging service simultaneously.
Example: Registering Multiple Sinks
Notes
- The
MinLogLevel
property allows you to filter which log levels your sink will process, but it only supports filtering for all levels at or above a certain severity (e.g., all logs that areCritical
or less severe). - If you want your sink to process only specific log levels (such as only
Informational
andCritical
), implement the filtering logic inside your sink’sWriteAsync
method. - The
WriteAsync
method is called asynchronously for each log message. - You can add multiple sinks; each will receive log messages independently if the log level matches their filter.
- Built-in outputs (console and file) are controlled by the global logger settings and are not affected by custom sink filters.
For advanced scenarios, you can implement sinks that send logs to cloud providers, external APIs, or batch/process logs as needed.