LibLog library analysis

Preliminary thinking:

  1. Microsoft.Logging is available?

  2. The need to provide a default Logger achieve? You do not need. 1, the user's own log function is turned on, set to open properties, to print out the corresponding log.

LibLog library analysis:

Classes of only needs to install the corresponding packet, then add the following fields in its own log classes.

private static readonly ILog logger = LogProvider.GetCurrentClassLogger()

or

private static readonly ILog Logger = LogProvider.For ()

No need to install other third-party libraries, at the time of logging, then simply call logger.Info or logger.Error library design is complete. Classes of course, may be provided to the client side of the interface is assumed to be a EnableLogger ()

On the client, users simply call this interface, it can automatically log record library, without any extra operation.

What are the principles which are to achieve that, the client side, in addition to calling a library of open log function interface, without any other operation.

In fact, we can analyze the design of integrated library, first initialize the object Logger

// GetCurrentClassLogger
static ILog GetCurrentClassLogger()
{
    var stackFrame = new StackFrame(1, false);
    return GetLogger(stackFrame.GetMethod().DeclaringType);
}

Gets the current class log, initiates a StackFrame, this should be in the Record Frame Call Stack. The following description is given official website:

A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread.

Then GetLogger ()

static ILog GetLogger(string name)
{
    var logProvider = CurrentLogProvider ?? ResolveLogProvider();
    return logProvider == null
        ? NoOpLogger.Instance
        : (ILog)new LoggerExecutionWrapper(logProvider.GetLogger(name), () => IsDisabled);
}

Focus ResolveLogProvider this method, first used the Lazy Initialization

private static readonly Lazy ResolvedLogProvider = new Lazy (ForceResolveLogProvider);

Then focus on the ForceResolveLogProvider, Lazy method receives the parameter type is Func Delegate type, i.e., a method of receiving a return value ILogProvider.

internal static ILogProvider ForceResolveLogProvider()
{
    try
    {
        foreach (var providerResolver in LogProviderResolvers)
        {
            if (providerResolver.Item1())
            {
                return providerResolver.Item2();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(
            "Exception occurred resolving a log provider. Logging for this assembly {0} is disabled. {1}",
            typeof(LogProvider).Assembly.FullName,
            ex);
    }
    return null;
}

The main method is to find whether the current process has been initiated in a log class, and if there is, then returned to the class. If not, an exception is thrown. Focus is LogProviderResolvers, this is an ancestral type, defined as follows:

static readonly List<Tuple<IsLoggerAvailable, CreateLogProvider>> LogProviderResolvers =
                new List<Tuple<IsLoggerAvailable, CreateLogProvider>>
        {
            new Tuple<IsLoggerAvailable, CreateLogProvider>(SerilogLogProvider.IsLoggerAvailable, () => new SerilogLogProvider()),
            new Tuple<IsLoggerAvailable, CreateLogProvider>(NLogLogProvider.IsLoggerAvailable, () => new NLogLogProvider()),
            new Tuple<IsLoggerAvailable, CreateLogProvider>(Log4NetLogProvider.IsLoggerAvailable, () => new Log4NetLogProvider()),
            new Tuple<IsLoggerAvailable, CreateLogProvider>(LoupeLogProvider.IsLoggerAvailable, () => new LoupeLogProvider()),
        };

After you finish configuring Provider, in fact, the library has acquired configured to log your local class, the class library when you want to print the log demand, then, to find a project for the log through reflection, then initialize to perform related operations.

DEMO Example:

Library: https://github.com/doublnt/dotnetcore/blob/master/LibLogSample/CommonLibLog.cs

Client: https://github.com/doublnt/dotnetcore/blob/master/CSharpFundamental/LibLog/ConsumerLibLog.cs

Guess you like

Origin www.cnblogs.com/xiyin/p/11093238.html