随笔 - 705  文章 - 0  评论 - 1103  阅读 - 138万 

2009-10-04_075710

image

image

一,下载并安装好Enterprise Library 4.1

二,新建一个Web应用程序

三,右键点击Web.Config 文件 使用 Edit Enterprise Library Configuration 可以编辑Web.Config,建立一个日志应用程序块

image

看到如下图的配置树

image

系统默认建立好一个Event Log 的Trace Listeners 名字为Formatted EventLog TraceListener

我们可以右键点击Trace Listeners 节点, 建立自己需要的Trace Listeners 比如建立一个 FlatFile TraceListener 建立完成可以设置他的属性,比如说日志文件的位置啊什么的,我全部都用默认也可以的,如下图

image

四,添加引用

image

五,添加生成事件脚本,复制Config,没有Config会报错

copy "$(ProjectDir)\*.config" "$(TargetDir)"

image

六,写代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace LoggingBlock
{
    public partial class _Default : System.Web.UI.Page
    {


        /// <summary>
        /// 定义严重级别的枚举
        /// </summary>
        public struct Priority
        {
            public const int Lowest = 0;
            public const int Low = 1;
            public const int Normal = 2;
            public const int High = 3;
            public const int Highest = 4;
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            /////创建一个日志项
            //LogEntry log = new LogEntry();

            //log.Message = this.TextBox1.Text;
            //log.Priority = Priority.Normal;

            /////写入日志
            //Logger.Write(log);
            Logger.Write(this.TextBox1.Text);
        }
    }
}

 

七,查看结果

在根目录中

image

在事件查看器中

image
示例源码下载:EL41Sample.rar
Enterprise Library 4.1 目录:Enterprise Library 4.1 快速使用图文笔记 目录

posted on   冯瑞涛  阅读(1433)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示