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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java中page的对象,page对象[通俗易懂]

    java中page的对象,page对象[通俗易懂]page对象是JSP九大内置对象之一。JSP全称JavaServerPage,是一种动态网页技术标准,以Java语言作为脚本语言。在JSP中预先定义了九个内置对象,这个九个内置对象不需要声明就可以在脚本代码和表达式中任意使用,九个内置对象分别是:request、response、session、application、out、pageContext、config、page、exception。pa…

    2022年7月27日
    10
  • 单向链表之删除节点(C语言实现)「建议收藏」

    单向链表之删除节点(C语言实现)「建议收藏」链表的创建查看删除节点就是将某一节点从链中摘除。将待删节点与其前一节点解除联系(中间或尾部)或本阶段删除(头节点),并释放相应空间(free)。删除的第一步是找到要删除的节点,同链表查找,如果找不到或链表为空,提示未找到,找到后根据情况删除此节点。删除节点两种情况:第一个节点,后面节点。步骤:1、链表为空:不用删除2、链表不为空:先循环找要删除的节点1)找到了1>找

    2022年10月24日
    0
  • LTE-TDD随机接入过程(3)-RAR(MSG2)以及MSG1的重传

    LTE-TDD随机接入过程(3)-RAR(MSG2)以及MSG1的重传本文涉及到的内容有:(1)UE在什么时候開始接收RAR(2)怎么确定RA-RNTI(3)UE没有收到RAR后的处理(4)RAR的格式1.UE监測RAR文章《LTE-TDD随机接入过程(2)-前导码Pr

    2022年8月4日
    3
  • 激光测距项目整体框图及原理

    激光测距项目整体框图及原理**前言:**因为前面几个星期在忙着准备一个面试,这个星期开始持续更新。。。今天的内容是相位式激光测距项目的一个整体框图及原理介绍,这部分文章链接将会加到之前的大纲中!大纲链接:目录大纲目录:1相位式激光测距原理2FFT与APFFT鉴相法1相位式激光测距原理激光测距相位法的原理这里就简单介绍一下,下面几张图片均来源于网络。这个项目主要是对激光强度进行调制的相位式激光测距(…

    2022年6月2日
    28
  • 批处理net命令集合

    批处理net命令集合批处理net命令集合netuse\\ip\ipc$””/user:””建立IPC空链接netuse\\ip\ipc$”密码”/user:”用户名”建立IPC非空链接 netuseh:\\ip\c$登陆后映射对方C:到本地为H: netuse\\ip\ipc$/del删除IPC链接 

    2022年5月28日
    41
  • GitHub 版本控制 项目托管 03 建立本地与远程的SSH连接

    GitHub 版本控制 项目托管 03 建立本地与远程的SSH连接

    2022年3月7日
    224

发表回复

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

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