Fork me on GitHub

Prism6下的MEF:添加Logger

上篇已经简单的构建了一个Prism的程序,现在我们需要添加一个Logger,Prism本身自带一个功能简单的TextLogger,但是我们希望能用.Net常用的Log4net。所以我们需要重载掉Bootstrapper中的CreateLogger方法。

新建一个PrismSample.Infrastructure.Logger项目

从nuget中引用log4net的,添加配置文件logging.confing:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net>
        <logger name="logerror">
        <level value="ERROR" />
        <appender-ref ref="ErrorAppender" />
        </logger>
        <logger name="loginfo">
        <level value="INFO" />
        <appender-ref ref="InfoAppender" />
        </logger>
        <logger name="logwarn">
        <level value="WARN" />
        <appender-ref ref="WarnAppender" />
        </logger>
        <logger name="logdebug">
        <level value="DEBUG" />
        <appender-ref ref="DebugAppender" />
        </logger>
        <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="App_Log\Error\ErrorLog.log" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="100" />
        <param name="MaximumFileSize" value="1MB" />
        <param name="RollingStyle" value="Size" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
        </layout>
        </appender>
        <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="App_Log\Info\InfoLog.log" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="100" />
        <param name="MaximumFileSize" value="1MB" />
        <param name="RollingStyle" value="Size" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
        </layout>
        </appender>
        <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="App_Log\Debug\DebugLog.log" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="100" />
        <param name="MaximumFileSize" value="1MB" />
        <param name="RollingStyle" value="Size" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
        </layout>
        </appender>
        <appender name="WarnAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="App_Log\Warn\WarnLog.log" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="100" />
        <param name="MaximumFileSize" value="1MB" />
        <param name="RollingStyle" value="Size" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
        </layout>
        </appender>
    </log4net>
</configuration>

新建Logger类:

using log4net;
using log4net.Config;
using Prism.Logging;
using System;
using System.IO;

namespace PrismSample.Infrastructure.Logger
{
    public class Logger : ILoggerFacade
    {
        private ILog _debugLogger;
        private ILog _errorLogger;
        private ILog _infoLogger;
        private ILog _warnLogger;

        public Logger()
        {
            var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "logging.config");
            XmlConfigurator.ConfigureAndWatch(logCfg);

            _debugLogger = LogManager.GetLogger("logdebug");
            _errorLogger = LogManager.GetLogger("logerror");
            _infoLogger = LogManager.GetLogger("loginfo");
            _warnLogger = LogManager.GetLogger("logwarn");
        }

        public void Log(string message, Category category, Priority priority)
        {
            switch(category)
            {
                case Category.Debug:
                    _debugLogger.Debug(message ?? "");
                    break;
                case Category.Exception:
                    _errorLogger.Error(message ?? "");
                    break;
                case Category.Info:
                    _infoLogger.Info(message ?? "");
                    break;
                case Category.Warn:
                    _warnLogger.Warn(message ?? "");
                    break;
            }
        }
    }
}

PrismSample添加Logger项目的引用,并重载掉Bootstrapper中的CreateLogger方法:

protected override ILoggerFacade CreateLogger()
{
    return new Logger();
}

Logger之所以不使用导入的方式是因为Logger的初始化在容器的初始化之前,我们预期Logger在程序已启动就配置完成,这样我们能记录容器初始化的过程。

测试Logger

修改ShellViewModel的构造函数如下:

[ImportingConstructor]
public ShellViewModel([Import("ShellView", typeof(IView))]IView view, [Import]ILoggerFacade logger)
{
    this.View = view;
    this._text = "Hello World";
    this.View.DataContext = this;


    logger.Log("ShellViewModel Created", Category.Info, Priority.None);
}

运行后打开Logger文件:
Image

现在整个项目的组织结构如下:
Image

小结

本文演示了log4net在Prism中的简单应用。
源码下载

参考信息

[Prism框架实用分享]如何在Prism应用程序中使用日志

posted @ 2016-07-15 08:10  tianjyan  阅读(1738)  评论(1编辑  收藏  举报