Enterprise Library-Logging block

Source: Internet
Author: User

 

1. Logging Block

Logging is an essential feature in almost all programs. Logging can help us Debug Programs, run time-consuming programs in the background program or Logging can help us to record whether the program runs correctly and whether exceptions are thrown.

Generally, Logging supports Logging to different places and flexibly controls whether logs are needed. The Logging Block of the Enterprise Library provides all of these features. Through the Enterprise Library, We can flexibly use the app. config/web. in the app, Logging provides the following methods to Log records:

1) The event log

2) An e-mail message

3) A database

4) A message queue

5) A text file

6) A Windows Management Instrumentation (WMI) event

7) Custom locations using application block extension points

 

2. Main objects and execution processes of the Logging Block

Main objects of the Logging Block:

1) log entity object. log entity can be understood as a log record; 2) log writer is a global log management object that contains all log-related operations;

3) log filter can be viewed as the attribute of log writer to control the behavior of log writer. For example, it can control whether log writer works and whether it only takes effect for certain priorities;

4) trace source/catagory source, used to manage multiple and organize multiple log record methods (trace listener). For example, a trace source can contain multiple record methods, such as logging to the db, and send emails to users at the same time. A log writer can contain multiple trace sources. Log writer has three special trace listeners: all event log source, unprocessed log source, warning and error log source;

5) trace listener: indicates the log record method, including db listener and email listener;

6) log formatter: used to define the log record format and content;

 

Logging Block execution process:

1) The client constructs the log entity and passes it to the log writer;

2) log writer uses log filter to filter log entity. Only the unfiltered log entity is actually recorded;

3) log writer transmits the log entity to all trace sources;

4) The trace source transmits the log entity to the managed trace listener;

5) trace listener records logs based on the configured log formatter;

The execution process of the logging block is as follows:

 

Configuration of three Logging Blocks

You can use the configuration tools provided by the enterprise database to generate corresponding configuration files,

 

For example, the above log writer Configuration:

Set whether the log can use the logging enable filter and only process the logging priority filter whose priority is 0 to 5;

Contains a trace source named General;

The general trace source contains a rolling flat file trace listener;

Rolling flat file trace listener uses text formatter to record logs;

 

The configured xml is as follows:

<Configuration>
<ConfigSections>
<Section name = "loggingConfiguration" type = "Microsoft. Practices. EnterpriseLibrary. Logging. Configuration. LoggingSettings,
Microsoft. Practices. EnterpriseLibrary. Logging,
Version = 5.0.414.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "requirePermission =" true "/>
</ConfigSections>
<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 = 5.0.414.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"
ListenerDataType = "Microsoft. Practices. EnterpriseLibrary. Logging. Configuration. RollingFlatFileTraceListenerData,
Microsoft. Practices. EnterpriseLibrary. Logging, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"
Formatter = "Text Formatter" rollInterval = "Day" rollSizeKB = "1024"
MaxArchivedFiles = "10"/>
</Listeners>
<Formatters>
<Add type = "Microsoft. Practices. EnterpriseLibrary. Logging. Formatters. TextFormatter,
Microsoft. Practices. EnterpriseLibrary. Logging, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"
Template = "Timestamp: {timestamp} {newline}
Message: {message} {newline}
Category: {category} {newline}
Priority: {priority} {newline}
EventId: {eventid} {newline}
Severity: {severity} {newline}
Title: {title} {newline}
Machine: {localMachine} {newline}
App Domain: {localAppDomain} {newline}
ProcessId: {localProcessId} {newline}
Process Name: {localProcessName} {newline}
Thread Name: {threadName} {newline}
Win32 ThreadId: {win32ThreadId} {newline}
Extended Properties: {dictionary ({key}-{value} {newline })}"
Name = "Text Formatter"/>
</Formatters>
<LogFilters>
<Add type = "Microsoft. Practices. EnterpriseLibrary. Logging. Filters. LogEnabledFilter,
Microsoft. Practices. EnterpriseLibrary. Logging, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"
Enabled = "true" name = "Logging Enabled Filter"/>
<Add type = "Microsoft. Practices. EnterpriseLibrary. Logging. Filters. PriorityFilter,
Microsoft. Practices. EnterpriseLibrary. Logging, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"
MaximumPriority = "5" name = "Priority Filter"/>
</LogFilters>
<CategorySources>
<Add switchValue = "All" name = "General">
<Listeners>
<Add name = "Rolling Flat File Trace Listener"/>
</Listeners>
</Add>
</CategorySources>
<SpecialSources>
<AllEvents switchValue = "All" name = "All Events"/>
<NotProcessed switchValue = "All" name = "Unprocessed Category"/>
<Errors switchValue = "All" name = "Logging Errors & amp; Warnings"/>
</SpecialSources>
</LoggingConfiguration>
</Configuration>

Sample Code

Using System; using System. Diagnostics;
Using Microsoft. Practices. EnterpriseLibrary. Logging;

Using Microsoft. Practices. EnterpriseLibrary. Common. Configuration;

Namespace MyCommon
{
Public sealed class MyLogger
{
# Region Static
Static MyLogger instance = null;
Static readonly object padlock = new object ();
Public static MyLogger Instance
{
Get
{
If (instance = null)
{
Lock (padlock)
{
If (instance = null)
{
Instance = new MyLogger ();
}
}
}
Return instance;
}
}
# Endregion

Private LogWriter lw = null;
Private MyLogger ()
{
Lw = EnterpriseLibraryContainer. Current. GetInstance <LogWriter> ();
}

Private void Log (string msg, TraceEventType tet)
{
If (lw. IsLoggingEnabled ())
{
LogEntry le = new LogEntry ();
Le. Message = msg;
Le. Severity = tet;
Lw. Write (le );
}
}

Public void Critical (string msg)
{
This. Log (msg, TraceEventType. Critical );
}
Public void Error (string msg)
{
This. Log (msg, TraceEventType. Error );
}
Public void Warning (string msg)
{
This. Log (msg, TraceEventType. Warning );
}
Public void Info (string msg)
{
This. Log (msg, TraceEventType. Information );
}

}
}

 

 

Complete!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.