NuGet package Ikc5.Prism.Common

Introduction

The post is devoted to NuGet package Ikc5.Prism.Common. The library contains useful classes for Prism applications.

Sources

The package is open-source project, full code is accessible on GitHub NuGet repository. Package is published at NuGet Gallery, and symbol’s package is pushed to SymbolSource.org.

Package

Package refers to Ikc5.TypeLibrary, and is used by Ikc5.Prism.Settings packages.

Logging

Package contains adapter classes that adapt Prism’s ILoggerFacade interface to ILogger interface, and the extended ILoggerTimeFacade interface to ITimestampLogger interface.

public interface ILoggerTimeFacade : ILoggerFacade
{
    /// <summary>
    /// Method logs message and write start time of the method.
    /// </summary>
    /// <param name="message">Message to be written.</param>
    /// <param name="category">Category of the message.</param>
    /// <param name="priority">Priority of the method.</param>
    /// <param name="propertyName">Property or method name; if is not empty, is added before the method.</param>
    void LogStart(string message, Category category, Priority priority, [CallerMemberName] string propertyName = null);

    /// <summary>
    /// Method logs message and write end time of the method.
    /// </summary>
    /// <param name="message">Message to be written.</param>
    /// <param name="category">Category of the message.</param>
    /// <param name="priority">Priority of the method.</param>
    /// <param name="propertyName">Property or method name; if is not empty, is added before the method.</param>
    void LogEnd(string message, Category category, Priority priority, [CallerMemberName] string propertyName = null);
}

Adapter classes are similar, so consider LoggerFaceadeAdapter class. Class has constructor that accepts parameter of ILoggerFacade type, keeps it in the variable and implements two interfaces:

/// <summary>
/// Adapter class that covers Prism's ILoggerFacade and shows it as ILogger.
/// </summary>
public class LoggerFacadeAdapter : ILoggerFacade, ILogger
{
    private LoggerFacadeAdapter()
    { }

    public LoggerFacadeAdapter(ILoggerFacade loggerFacade)
        : this()
    {
        loggerFacade.ThrowIfNull(nameof(loggerFacade));
        LoggerFacade = loggerFacade;
    }

    protected ILoggerFacade LoggerFacade { get; }

    #region Implementation of ILoggerFacade

    public void Log(string message, Category category, Priority priority)
    {
        LoggerFacade.Log(message, category, priority);
    }

    #endregion

    #region Implementation of ILogger

    public void Log(string message, TypeLibrary.Logging.Category category, TypeLibrary.Logging.Priority priority)
    {
        LoggerFacade.Log(message, category.ToPrismCategory(), priority.ToPrismPriority());
    }

    #endregion
}

Library contains class with extensions, that simplfies useful scenarios of logging, like log message with default priority and category, or log thrown exception:

/// <summary>
/// Short call of ILoggerFacade.Log method.
/// </summary>
/// <param name="logger">Logger object from Unity Container.</param>
/// <param name="message">Message to be written.</param>
/// <param name="category">Category of the message.</param>
/// <param name="priority">Priority of the method.</param>
/// <param name="propertyName">Property or method name; if is not empty, is added before the method.</param>
/// <returns>Logger object.</returns>
public static ILoggerFacade Log(this ILoggerFacade logger, string message,
    Category category = Category.Debug, Priority priority = Priority.None,
    [CallerMemberName] string propertyName = null);

/// <summary>
/// Short call that logs information about exception.
/// </summary>
/// <param name="logger">Logger object from Unity Container.</param>
/// <param name="ex">Exception that was thrown.</param>
/// <param name="category">Category of the message.</param>
/// <param name="propertyName">Property or method name; if is not empty, is added before the method.</param>
/// <returns>Logger object.</returns>
public static ILoggerFacade Exception(this ILoggerFacade logger,
    System.Exception ex,
    Category category = Category.Exception,
    [CallerMemberName] string propertyName = null);

/// <summary>
/// Extension detects derived logger with possibility to write start and end time of the method.
/// If logger has such possibility, it call correspond method.
/// </summary>
/// <param name="logger">Logger object from Unity Container.</param>
/// <param name="message">Message to be written.</param>
/// <param name="category">Category of the message.</param>
/// <param name="priority">Priority of the method.</param>
/// <param name="propertyName">Property or method name; if is not empty, is added before the method.</param>
/// <returns></returns>
public static ILoggerFacade LogStart(
    this ILoggerFacade logger, string message,
    Category category = Category.Info, Priority priority = Priority.None,
    [CallerMemberName] string propertyName = null);

/// <summary>
/// Extension detects derived logger with possibility to write start and end time of the method.
/// If logger has such possibility, it call correspond method.
/// </summary>
/// <param name="logger">Logger object from Unity Container.</param>
/// <param name="message">Message to be written.</param>
/// <param name="category">Category of the message.</param>
/// <param name="priority">Priority of the method.</param>
/// <param name="propertyName">Property or method name; if is not empty, is added before the method.</param>
/// <returns></returns>
public static ILoggerFacade LogEnd(
    this ILoggerFacade logger, string message,
    Category category = Category.Info, Priority priority = Priority.None,
    [CallerMemberName] string propertyName = null);

History

  • 2016.12.01 – Publish the version 1.0.0.0 package;
  • 2017.01.11 – write this post, as a short description.

1. All used IP-addresses, names of servers, workstations, domains, are fictional and are used exclusively as a demonstration only.
2. Information is provided «AS IS».

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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