首页 技术 正文
技术 2022年11月12日
0 收藏 925 点赞 3,793 浏览 2163 个字

前言

在Prism中有关日志的命名空间:

Microsoft.Practices.Prism.Logging

在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可以使用自定义的Logger(比如Log4Net等),但所有的Logger都比须实现接口 ILoggerFacade 。下面就以Log4Net为例,实现一个自定义的Logger,并将它应用到Demo中。

有关Log4Net日志组件的使用方法这里就不介绍了,下面列出几篇介绍Log4Net的资料:

1. http://logging.apache.org/log4net/

2. http://blog.csdn.net/zhoufoxcn/article/details/2220533

3. http://blog.csdn.net/zhoufoxcn/article/details/6029021

Log4Net的配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net debug="true">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="d:\log.txt"/>
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%l %d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="LogFileAppender"/>
</root>
</log4net>
</configuration>

以上是本例对Log4Net简单的配置

实现接口ILoggerFacade

新建一个类CustomLogger,并实现接口ILoggerFacade

    public class CustomLogger : ILoggerFacade
{
private readonly ILog s_ILogger = null; public CustomLogger()
{
if (s_ILogger == null)
{
s_ILogger = LogManager.GetLogger("Logger");
log4net.Config.XmlConfigurator.Configure();
}
} #region ILoggerFacade 成员 public void Log(string message, Category category, Priority priority)
{
if (string.IsNullOrEmpty(message))
return; switch (category)
{
case Category.Debug:
this.Debug(message);
break;
case Category.Exception:
this.Error(message);
break;
case Category.Info:
this.Info(message);
break;
case Category.Warn:
this.Fatal(message);
break;
}
} #endregion void Debug(string message) { s_ILogger.Debug(message); } void Info(string message) { s_ILogger.Info(message); } void Error(string message) { s_ILogger.Error(message); } void Fatal(string message) { s_ILogger.Fatal(message); }
}

简单的实现了ILoggerFacade接口,可参考Prism中TextLogger

将自定义Logger加载到主程序中

在Prism程序的启动器内Bootstrapper,重写CreateLogger()方法

        protected override Microsoft.Practices.Prism.Logging.ILoggerFacade CreateLogger()
{
return new CustomLogger();
}

如何使用日志

在ViewModel或者其他逻辑类,服务类中,定义私有变量,并声明导入特性

        [Import]
private ILoggerFacade _Logger;
        public void OnImportsSatisfied()
{
this._Logger.Log("Import Complete", Category.Info, Priority.None); if (PlugIns == null)
...
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298