惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - pdfw

如何预编译ASP.Net程序 asp.net 输入框在chrome中无法关闭自动提示 sql server不能删除数据库,显示错误:正在使用 如何只更新datetime类型字段中的日期 SQL Server插入或修改数据是中文乱码的问题 怎么解决安装SqlServer2008总是提示Restart computer as failed 分页查询的SQL语句 【转】国外C#开源系统一览表 ,C# Open Source 【转】.NET试题总结二 【转】.NET试题总结一 窗口之间传递消息的一个方法 Dataset Designer在VS 2008里面不工作的解决办法 Flex中查找XML节点 - pdfw - 博客园 c#如何监视文件或者文件夹的变化 - pdfw - 博客园 如何在非英文环境中正确显示数字 SQL Server Express中连接字符串的问题 wpf制作毛玻璃效果按钮的代码 WPF中用于Path的Geometry Mini-Language 如何跨线程访问UI控件
如何使用Microsoft Enterprise Library里面的Log功能
pdfw · 2013-06-07 · via 博客园 - pdfw

以VS2012里面建立的一个控制台程序为例

1. 安装Microsoft Enterprise Library里面的Logging Application模块。

在需要使用Log功能的项目上面右键,选择Manage NuGet Packeages...

2. 在Manage NuGet Packeages窗口里面找到Enterprise Library - Logging Application Block,然后安装。

安装成功以后,项目引用中会增加两个新的引用。

3. 我们需要对App.config文件进行配置。在这里我们使用配置编辑工具:Microsoft.Practices.EnterpriseLibrary.ConfigConsoleV6.vsix。这个工具的下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=38789

4. 配置App.config文件。右键App.config文件选择Edit configuration file v6,打开配置工具窗口。

5. 选择菜单命令Block -> Add Logging Settings

6. 在Logging Target Listeners里面点加号按钮,然后选择Add Rolling Flat File Trace Listener(生成可以进行自动分割的文本文件)。

7. 一般需要设置的参数有:Asynchronous(选true则进行异步log), File Exist Behavior(选), File Name, Formatter Name, Max Archived Files, Roll Interval, Roll Size KB。

其中Formatter Name的值从Log Message Formatters中生成的值中选取。

8. 生成 Message Format。在Log Message Formatters中点击加号按钮,选择Add Text Formatter

点击Template右侧的...按钮,打开Template Editor对话框,对Template的内容进行编辑

编辑后在App.config中生成的xml代码如下:

<formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp(local:MM/dd/yyyy HH:mm:ss.fffffff)}{tab}Category: {category}{tab}Severity: {severity}{tab}App Domain: {localAppDomain}{tab}Message location: {dictionary({key} - {value}{tab})}Message: {message}{tab}"
        name="LogFormatter" />
    </formatters>

Logging formatter

9. 在窗口左侧区域中点击Cotegories右边的加号按钮。生成一个新的Category

10. 在新生成的Category区域中修改Name属性,然后点击Listeners右边的加号按钮,选择在Logging Target Listeners区域中已经生成的Listener。

11. 对已经进行的设置保存

12. 写个简单的测试程序看看生成的Log效果如何

using Microsoft.Practices.EnterpriseLibrary.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LogTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            Logger.SetLogWriter(new LogWriterFactory().Create());
            for (int i = 0; i < 10; i++)
            {
                Thread thread = new Thread(() =>
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            Logger.Write("测试", "MyLog", 0, 0, System.Diagnostics.TraceEventType.Information);
                            Dictionary<string, object> dic = new Dictionary<string, object>()
                                {
                                    { "Projec", "world" }, 
                                    { "Method", "hello" }, 
                                };
                            Logger.Write("test1", "MyLog", 0, 0, System.Diagnostics.TraceEventType.Error, "", dic);
                        }
                    });
                thread.Start();
            }
            Console.ReadKey();
        }
    }

}

Log Testing Code