Before reading this article, please read: Enterprise Library 2.0--Logging Application Block (upper)
In the previous article we introduced how to configure the logging Application block, this article will mainly introduce the logging Application Block's basic operation as well as the formatter and the trace Listeners custom method, First, let's look at how to write an event log into a text file.
Assuming that we have configured the logging Application Block according to the previous operation, the information in the configuration file is as follows:
<loggingconfiguration name= "Logging Application Block" tracingenabled= "true"
Defaultcategory= "General" logwarningswhennocategoriesmatch= "true" >
<listeners>
<add filename= "Trace.log" header= "----------------------------------------"
Footer= "----------------------------------------" formatter= "SHY520 Formatter"
Listenerdatatype= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, version=2.0.0.0, culture=neutral, Publickeytoken=null "
traceoutputoptions= "None" type= " Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, version=2.0.0.0, culture=neutral, Publickeytoken=null "
Name= "SHY520 Listeners"/>
<add source= "Enterprise Library Logging" formatter= "Text Formatter"
log= "Application" machinename= "listenerdatatype=" Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, version=2.0.0.0, culture=neutral, Publickeytoken=null "
traceoutputoptions= "None" type= " Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, version=2.0.0.0, culture=neutral, Publickeytoken=null "
Name= "formatted EventLog TraceListener"/>
</listeners>
<formatters>
<add template= "Timestamp: {Timestamp} message: {message} Category: {Category} Priority: {Priority} EventId:
{EventID} Severity: {Severity} Title:{title} Machine: {Machine} application Domain: {appDomain} Process Id:
{ProcessID} Process name: {processname} Win32 thread Id: {win32threadid} thread Name: {threadname} Extended Properties:
{Dictionary ({key}-{value})} "
Type= "Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, version=2.0.0.0,
Culture=neutral, Publickeytoken=null "
Name= "Text Formatter"/>
<add template= "Timestamp: {Timestamp} message: {message} Category: {Category} Priority: {Priority} EventId:
{EventID} Severity: {Severity} Title:{title} Machine: {Machine} application Domain: {appDomain} Process Id:
{ProcessID} Process name: {processname} Win32 thread Id: {win32threadid} thread Name: {threadname} Extended Properties:
{Dictionary ({key}-{value})} "
Type= "Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, version=2.0.0.0,
Culture=neutral, Publickeytoken=null "
Name= "SHY520 Formatter"/>
</formatters>
<logFilters>
<add categoryfiltermode= "allowallexceptdenied" type= " Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter,
Microsoft.Practices.EnterpriseLibrary.Logging, version=2.0.0.0, culture=neutral, Publickeytoken=null "
Name= "Category Filter"/>
</logFilters>
<categorySources>
<add switchvalue= "All" Name= "General" >
<listeners>
<add name= "SHY520 Listeners"/>
</listeners>
</add>
</categorySources>
<specialSources>
<allevents switchvalue= "All" name= "all Events"/>
<notprocessed switchvalue= "All" name= "unprocessed Category"/>
<errors switchvalue= "All" name= "Logging errors & Warnings ">
<listeners>
<add name= "formatted EventLog TraceListener"/>
</listeners>
</errors>
</specialSources>
</loggingConfiguration>