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


相关推荐

  • 【机器学习笔记】有监督学习和无监督学习

    【机器学习笔记】有监督学习和无监督学习有监督学习和无监督学习(一)有监督学习(二)无监督学习(三)二者的区别(四)如何在两者中选择合适的方法(一)有监督学习概念:通过已有的训练样本去训练得到一个最优模型,再利用这个模型将所有的输入映射为相应的输出,对输出进行简单的判断从而实现预测和分类的目的,也就具有了对未知数据进行预测和分类的能力。简单来说,就像有标准答案的练习题,然后再去考试,相比没有答案的练习题然后去考试准确率更高。监督学…

    2022年5月28日
    52
  • Android开发调节屏幕亮度

    Android开发调节屏幕亮度

    2022年1月16日
    34
  • 妙用AccessibilityService黑科技实现微信自动加好友拉人进群聊[通俗易懂]

    妙用AccessibilityService黑科技实现微信自动加好友拉人进群聊[通俗易懂]妙用AccessibilityService黑科技实现微信自动加好友拉人进群聊标签:2018引言:在上上周的周六和周日,我发了两篇利用itchat实现微信机器人的文章(Python):小猪的Python学习之旅——18.Python微信转发小宇宙早报小猪的Python学习之旅——19.Python微信自动好友验证,自动回复,发送群聊链接通过把脚本挂到服务器上…

    2022年6月4日
    98
  • 从零到熟悉,带你掌握Python len() 函数的使用

    从零到熟悉,带你掌握Python len() 函数的使用摘要:本文为你带来如何找到长度内置数据类型的使用len()使用len()与第三方数据类型提供用于支持len()与用户定义的类。本文分享自华为云社区《在Python中使用len()函数》,

    2022年7月5日
    19
  • 写给大忙人看的 – Java中图片压缩上传至MinIO服务器(4)[通俗易懂]

    写给大忙人看的 – Java中图片压缩上传至MinIO服务器(4)[通俗易懂]之前文章已经介绍了MinIO的环境搭建,已经对文件的上传下载方法,本篇文章一起与大家来学习图片压缩上传的方法1、背景最近客户总抱怨APP中图片显示较慢,升级服务器带宽又没有多的预算,查看原因,是因为现在大家都是用的智能手机拍照,拍出来的照片小则2-3M,大则十几M,所以导致图片显示较慢。思考再三,决定将图片进行压缩再上传图片服务器来解决图片显示慢的问题

    2022年6月18日
    69
  • 多重共线性检验-方差膨胀系数(VIF)

    多重共线性检验-方差膨胀系数(VIF)  方差膨胀系数(varianceinflationfactor,VIF)是衡量多元线性回归模型中复(多重)共线性严重程度的一种度量。它表示回归系数估计量的方差与假设自变量间不线性相关时方差相比的比值。  多重共线性是指自变量之间存在线性相关关系,即一个自变量可以是其他一个或几个自变量的线性组合。若存在多重共线性,计算自变量的偏回归系数时矩阵不可逆。其表现主要有:整个模型的方差分析…

    2022年6月11日
    147

发表回复

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

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