Enterprise Library:日志处理步骤

An overview of the logging process and the objects in the Logging block

Ff953185.56cf3e74-bacb-4bc5-a77d-87246ef38e98-thumb(en-us,PandP.50).png

 

 

Stage

Description

Creating the Log Entry

The user creates a LogWriter instance, uses it to create a new LogEntry, and passes it to the Logging block for processing. Alternatively, the user can create a new LogEntry explicitly, populate it with the required information, and use a LogWriter to pass it to the Logging block for processing.

Filtering the Log Entry

The Logging block filters the LogEntry (based on your configuration settings) for message priority, or categories you added to the LogEntry when you created it. It also checks to see if logging is enabled. These filters can prevent any further processing of the log entries. This is useful, for example, when you want to allow administrators to enable and disable additional debug information logging without requiring them to restart the application.

Selecting Trace Sources

Trace sources act as the link between the log entries and the log targets. There is a trace source for each category you define in the logging block configuration; plus, there are three built-in trace sources that capture all log entries, unprocessed entries that do not match any category, and entries that cannot be processed due to an error while logging (such as an error while writing to the target log).

Selecting Trace Listeners

Each trace source has one or more trace listeners defined. These listeners are responsible for taking the log entry, passing it through a separate log formatter that translates the content into a suitable format, and passing it to the target log. Several trace listeners are provided with the block, and you can create your own if required.

Formatting the Log Entry

Each trace listener can use a log formatter to format the information contained in the log entry. The block contains log message formatters, and you can create your own formatter if required. The text formatter uses a template containing placeholders that makes it easy to generate the required format for log entries.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 .NET Core 中使用 Enterprise Library,需要执行以下步骤: 1. 安装 NuGet 包:在项目中安装 Enterprise Library 的 NuGet 包。在 Visual Studio 中,右键单击项目并选择“Manage NuGet Packages”,然后搜索“EnterpriseLibrary”并安装。 2. 添加配置文件:在项目中添加一个名为“app.config”的配置文件,并将以下代码添加到文件中: ``` <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> </configSections> <enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source"> <sources> <add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" filePath=".\App.config" /> </sources> <redirectSections> <section name="loggingConfiguration" sourceName="File Configuration Source" /> </redirectSections> </enterpriseLibrary.ConfigurationSource> <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"> <listeners> <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="C:\logs\myapp.log" footer="--------------------------" formatter="Text Formatter" header="--------------------------" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="1024" timeStampPattern="yyyy-MM-dd" /> </listeners> <formatters> <add name="Text Formatter" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="{timestamp} [{category}] {message}" /> </formatters> <categorySources> <add switchValue="All" name="General"> <listeners> <add name="Rolling Flat File Trace Listener" /> </listeners> </add> </categorySources> </loggingConfiguration> </configuration> ``` 这个配置文件包含一个名为“Rolling Flat File Trace Listener”的日志记录器,它将日志记录到文件中。你可以根据自己的需要更改此配置。 3. 创建 Enterprise Library 实例:在代码中创建 Enterprise Library 实例,并使用它来记录日志。以下是一个示例: ``` using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging; ILogger logger = EnterpriseLibraryContainer.Current.GetInstance<ILogger>(); logger.Write("Hello, world!"); ``` 这将使用配置文件中指定的日志记录器记录一条消息。 希望这可以帮助你配置 Enterprise Library 在 .NET Core 中运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值