J4JLogger: Major Release

Earlier versions of J4JLogger tried to “simplify” defining and configuring channels/sinks. However, as I continued to use the library — and learned more about Serilog — I came to realize a better approach was to stick as closely as possible to the “Serilog way” of doing things. Because having to remember two similar-but-different configuration approaches quickly gets confusing.

To that end, the entire concept of channels (which were related to sinks) is gone. Instead, you configure J4JLogger-specific features (there’s currently only one, a facility for sending log events as text messages via SMS) through a configuration object and do everything else the way you always have with Serilog, by using its LoggerConfiguration API.

Doing that requires you configure the LoggerConfiguration instance embedded within an instance of J4JLoggerConfiguration. That’s done like this:

var loggerConfig = new J4JLoggerConfiguration();

var outputTemplate = loggerConfig.GetOutputTemplate( true );

loggerConfig.SerilogConfiguration
    .WriteTo.Debug( outputTemplate: outputTemplate )
    .WriteTo.Console( outputTemplate: outputTemplate )
    .WriteTo.File(
        path: Path.Combine( Environment.CurrentDirectory, "log.txt" ),
        outputTemplate: loggerConfig.GetOutputTemplate( true ),
        rollingInterval: RollingInterval.Day );

var logger = loggerConfig.CreateLogger();
logger.SetLoggedType( typeof(Program) );

The SerilogConfiguration property on the J4JLoggerConfiguration instance gives you access to the traditional Serilog::LoggerConfiguration API.

You may wonder why there’s a call to loggerConfig to generate an output template to use in configuring the various Serilog sinks. The answer is simple: in order for the calling context information (e.g., calling method name) to appear in the log there has to be a placeholder for it the output template Serilog uses…and the default template obviously won’t contain it.

I also eliminated the need to use dependency injection to configure the logger.

The NetEventSink is incorporated into the library in a different way. When it’s included as a sink the events it generates are raised by IJ4JLogger. This simplified setting up the sink.

Adding NetEventSink is done by calling the extension method NetEvent() on an instance of J4JLoggerConfiguration:

loggerConfig.NetEvent( outputTemplate: outputTemplate,
    restrictedToMinimumLevel: NetEventConfiguration.MinimumLevel );

Keep in mind that if you don’t add it as a sink no LogEvents will ever be raised by IJ4JLogger.

For more information on this release, how to use the library, etc., please consult the following:

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Archives
Categories