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


相关推荐

  • 短信验证码的作用及安全性

    短信验证码的作用及安全性在每个人都可以参与编辑的Web2.0时代,人人都在使用和注册各种APP账号,而验证码短信就是必不可少的认证工具!所以,验证码短信逐步占据了短信行业中的重要份额。而短信验证码的作用是:1、作为身份凭证首先,验证码是商家给用户验证身份的一个凭证,通过手机短信发送验证码,是最普遍、最安全验证用户真实身份的方式。短信验证码广泛应用于用户注册、密码找回、登陆保护、身份认证、随机密码、交易确认等应用场景。2、…

    2022年7月14日
    21
  • 爬虫为啥需要大量的ip_简述网络爬虫的工作原理

    爬虫为啥需要大量的ip_简述网络爬虫的工作原理http/https等爬虫代理ip的基本实现原理爬虫工作者在试用爬虫进行数据搜集的过程中经常会遇到这样的情况,刚开始的时候爬虫的运行情况是正常的,数据的抓取状况也在有条不紊的进行着,然而可能你一眼照顾不到就会出现错误,比如403Forbidden,这时候你打开网页的话,网页上面会提示你,“您的IP访问频率太高”这样的字眼。出现这种现象的原因就是被访问网站采取了反爬虫机制,比如,服务器会检测某个IP在单位时间内的请求次数,如果超过了这个阈值,就会直接拒绝服务,返回一些错误信息,这种情况可以称为封IP。那

    2022年10月8日
    2
  • MySQL多表查询核心优化

    MySQL多表查询核心优化在一般的项目开发中,对数据表的多表查询是必不可少的。而对于存在大量数据量的情况时(例如百万级数据量),我们就需要从数据库的各个方面来进行优化,本文就先从多表查询开始。

    2022年6月2日
    38
  • C++宏和枚举

    宏我们的计算器程序,用1234对应加减乘除,对于人阅读很产生一点障碍。隔一个月后再看此代码可能想不起是0123还是1234了,还得去代码中查找,如果能为代表四则运算的四个数取个有意义的别名就好了,一

    2021年12月24日
    52
  • IplImage中的widthStep大小计算及原理[通俗易懂]

    IplImage中的widthStep大小计算及原理[通俗易懂]一直以为IplImage结构体中的widthStep元素大小等于width*nChannels,大错特错!查看OpenCV2.1的源码,在src/cxcore/cxarray.cpp文件中,找到cvInitImageHeader函数,函数中对widthStep大小赋值如下:image->widthStep=(((image->width*image->nChannels*

    2022年4月30日
    50
  • RFID-RC522射频

    RFID-RC522射频与Arduino的接线方法:下载库:  搜索RC522转载于:https://www.cnblogs.com/liming19680104/p/11577035.html…

    2022年7月14日
    16

发表回复

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

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