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


相关推荐

  • 【转载】win7下IIS的安装和配置图文教程

    【转载】win7下IIS的安装和配置图文教程

    2021年11月18日
    37
  • 干货|手把手教你写一个串口调试助手「建议收藏」

    干货|手把手教你写一个串口调试助手「建议收藏」摘要:前段时间发布了一个用QT写的串口调试助手,很多小伙伴在后台留言要源码。其实网上有很多免费开源的用QT的上位机,大家搜一下就能找到,为了大家方便学习QT以及如何写一个上位机,今天推荐一下学习资源,顺带带大家写一个非常简单的串口调试助手。相信很多小伙伴还没有接触过QT,如果想用QT写一个调试助手,首先是会一点C++语法。了解即可,也就是看得懂C++的代码。只要能看懂简单的C+++语法,就能很快的写一个串口调试助手。先推荐两个视频教程,感兴趣可以看看!1、B站Jomse工看完你基本知道串口调试助手

    2022年5月9日
    87
  • 每天一道算法_2_求高精度幂

    今天这个可能有点偷懒,学c++的时候就知道大数计算很复杂,现在一直用java,有幸有大数的方法可以用那么今天的算法就用java偷懒过去吧Description对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。 现在要你解决的问题是:对一个实数R( 0.0 n),其中n 是整数并且 0 InputT输入包括多组 R 和 n。

    2022年3月10日
    41
  • ride运行报错_chrome OS

    ride运行报错_chrome OShttp://chromedriver.storage.proxy.ustclug.org/index.html下载地址,注意需要与chrome版本对应chromedriver下载解压后,放在chrome安装目录下,查看安装目录chrome://version/最后配置环境变量,chrome安装目录配置在path后重启(我是win10,开始配置在系统变量中重启后无效,后来在用户变量…

    2025年6月3日
    4
  • Oracle数据库存储number类型数据「建议收藏」

    Oracle数据库存储number类型数据「建议收藏」转载自初一七月Oracle数据类型之numberoracle的number类型是oracle的内置类型之一,是oracle的最基础数值数据类型。在9iR2及其以前的版本中只支持一种适合存储数值数据的固有数据类型,在10g以后,才出现了两种新的数值类型,即推出本地浮点数据类型(NativeFloating-PointDataTypes):BINARY_FLOAT(单精度32位)和BINAR…

    2022年7月24日
    5
  • 2021python激活码【在线注册码/序列号/破解码】[通俗易懂]

    2021python激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    55

发表回复

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

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