Microsoft Enterprise Library: Logging 模块「建议收藏」

Microsoft Enterprise Library: Logging 模块「建议收藏」MicrosoftEnterpriseLibrary中的Logging模块主要用来记录日志,它可以将日志存储在不同的介质中:文本文件,WindowsEvent,邮件,MSMQ,DataBase,Xml等等。当然它还提供了扩展功能,通过扩展Logging模块的Listener类,我们就能将日志记录在我们需要的地方了。   虽然MicrosoftEnterpriseLibrary很庞大,但

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

    Microsoft Enterprise Library 中的Logging模块主要用来记录日志,它可以将日志存储在不同的介质中:文本文件,Windows Event,邮件,MSMQ,DataBase,Xml等等。当然它还提供了扩展功能,通过扩展Logging模块的Listener类,我们就能将日志记录在我们需要的地方了。

   虽然Microsoft Enterprise Library很庞大,但是却非常容易使用。接下来我们就来看个简单的例子吧。首先来看看Microsoft Enterprise Library 中Logging 模块的配置信息吧。

配置信息

Microsoft Enterprise Library: Logging 模块「建议收藏」

图1

Microsoft Enterprise Library: Logging 模块「建议收藏」

图2

Microsoft Enterprise Library: Logging 模块「建议收藏」

图3

 SimpleDemo

          Microsoft Enterprise Library 提供的Listener, Filter, Formatter能满足我们大部分的需求,当然也会有特殊的情况。还好类库为我们提供了扩展接口,我们只有实现这些接口就可以制作属于自己的Listener, Filter, Formatter了。我们来看个简单的DEMO吧:

Microsoft Enterprise Library: Logging 模块「建议收藏」

图4

Microsoft Enterprise Library: Logging 模块「建议收藏」

图5

 

Custom Listener (自定义监听)

Microsoft Enterprise Library: Logging 模块「建议收藏」

图6

Microsoft Enterprise Library: Logging 模块「建议收藏」

图7

Microsoft Enterprise Library: Logging 模块「建议收藏」

图8

Microsoft Enterprise Library: Logging 模块「建议收藏」

图9

Microsoft Enterprise Library: Logging 模块「建议收藏」

图10

Microsoft Enterprise Library: Logging 模块「建议收藏」

图11

 

 

 

Custom Formatter(自定义格式器)

Microsoft Enterprise Library: Logging 模块「建议收藏」

图12

Microsoft Enterprise Library: Logging 模块「建议收藏」

图13

Microsoft Enterprise Library: Logging 模块「建议收藏」

图14

Microsoft Enterprise Library: Logging 模块「建议收藏」

图15

 

 

 

Custom Filter(自定义过滤器)

Microsoft Enterprise Library: Logging 模块「建议收藏」

图16

Microsoft Enterprise Library: Logging 模块「建议收藏」

图17

Microsoft Enterprise Library: Logging 模块「建议收藏」

图18

 

使用到的代码

SimpleDemo

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace SimpleLoggingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            LogWriter log = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            log.Write("Hello Microsoft Enterprise Library");
            log.Dispose();
        }
    }
}

CustomListener

using System;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.IO;
using Microsoft.Win32;
using System.Collections.Generic;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace Logging
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class CustomListener:CustomTraceListener
    {

        public override void Write(string message)
        {
            WriteMsgInRegistryKey(message);
        }

        public override void WriteLine(string message)
        {
            WriteMsgInRegistryKey(message);
        }

        private void WriteMsgInRegistryKey(string msg)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey("software", true);
            RegistryKey microsoft = software.OpenSubKey("microsoft", true);
            RegistryKey entryDir = microsoft.CreateSubKey("Entry Dir", RegistryKeyPermissionCheck.ReadWriteSubTree);

            if (entryDir != null)
            {
                int entryNum = entryDir.GetValueNames().Length;
                entryDir.SetValue(string.Format("No{0}", entryNum), msg, RegistryValueKind.String);
                entryDir.Close();
            }
            microsoft.Close();
            software.Close();
           

        }
        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {
            if (this.Formatter != null)
            {
                this.Write(this.Formatter.Format(data as LogEntry));
            }
            else
            {
                this.Write(data.ToString());
            }
        }
    }
}

CustomFormatter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;

namespace Logging
{
    [ConfigurationElementType(typeof(CustomFormatterData))]
    public class CustomTextFormatter:ILogFormatter
    {
        private NameValueCollection _Dictionary = null;
        public CustomTextFormatter(NameValueCollection dictionary) 
        {
            this._Dictionary = dictionary;
        }
        
        #region ILogFormatter 成员

        public string Format(LogEntry log)
        {
            return log.ToString() + UtileLibrary.PrintNameValueCollection(_Dictionary) +"\n By Custom Formatter!\n";
        }

        #endregion
    }
}

CustomFilter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

namespace Logging
{
    [ConfigurationElementType(typeof(CustomLogFilterData))]
    public class CustomLogFilter:ILogFilter
    {
        public CustomLogFilter(NameValueCollection dictionary)
        { 
        
        }

        #region ILogFilter 成员

        public bool Filter(LogEntry log)
        {
            if (log.Message.Contains("fuck"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public string Name
        {
            get { return "GhostHouse's Door"; }
        }

        #endregion
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/179915.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【技能树】预备知识-Python简介「建议收藏」

    【技能树】预备知识-Python简介「建议收藏」目录简介发展历史发展历程GuidovanRossum(吉多·范罗苏姆)人物经历主要成就ABC语言GNU特点优点缺点和其他语言区别Hello,World!简介Python是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。Python的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结构。Python是一种解释型语言:这意味着开发过程中没有了编译这个环节。类似于PHP和Perl语言。Python是交互式语言

    2022年5月20日
    33
  • 滚动条三要素scrollTop clientHeight scrollHeight

    滚动条三要素scrollTop clientHeight scrollHeight<!DOCTYPEhtml><html> <head> <metacharset=”utf-8″> <title></title> <scriptsrc=”js/jquery-3.3.1.min.js”type=”text/javascript”charset=”utf-8″></s…

    2022年7月23日
    7
  • VS无法打开源文件及无法打开链接库文件的解决方法

    VS无法打开源文件及无法打开链接库文件的解决方法一、无法打开源文件依次点击“项目——配置属性——C/C++——常规”,在“附加包含目录”中加入.h文件所在的文件夹路径二、无法打开文件“XXX.lib”1、依次点击“项目——配置属性——链接器——常规”,在“附加库目录”中加入.lib所在的文件夹的路径 2、在“链接器”中找到“输入”,在“附加依赖项”中加入需要加入的xxx.lib;(要用;和其他链接库分

    2022年6月22日
    774
  • 王立平–include在Android应用

    王立平–include在Android应用

    2021年12月30日
    41
  • Android最全的屏幕适配

    Android最全的屏幕适配转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的官方文档为基础,全面而深入的讲解了Android屏幕适配的原因、重要概念、解决方案及最佳实践,我相信如果你能认真的学习本文,对于Android的屏幕适配,你将有所收获!Android屏幕适配出

    2022年5月13日
    37
  • 转:ATI显卡的机器上安装Linux花屏解决办法

    转:ATI显卡的机器上安装Linux花屏解决办法

    2021年9月5日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号