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

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - 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