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

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

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

如何使用Microsoft Enterprise Library里面的Log功能

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

如何使用Microsoft Enterprise Library里面的Log功能

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

如何使用Microsoft Enterprise Library里面的Log功能

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,打开配置工具窗口。

如何使用Microsoft Enterprise Library里面的Log功能

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

如何使用Microsoft Enterprise Library里面的Log功能

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

如何使用Microsoft Enterprise Library里面的Log功能

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

如何使用Microsoft Enterprise Library里面的Log功能

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

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

如何使用Microsoft Enterprise Library里面的Log功能

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

如何使用Microsoft Enterprise Library里面的Log功能

编辑后在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

相关文章:

  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
猜你喜欢
  • 2021-06-21
  • 2022-12-23
  • 2021-12-11
  • 2021-08-25
  • 2021-12-07
  • 2022-12-23
  • 2022-02-17
相关资源
相似解决方案