Microsoft Enterprise Library is a collection of application
blocks designed to assist developers with common enterprise development
challenges. Application blocks are a type of guidance, provided as source code
that can be used "as is," extended, or modified by developers for use
in enterprise development projects.
Installation
- Enterprise Library & Source Code
- Enterprise Library Optional Update & Source Code
- Enterprise Library Documentation
Configuration
Launch the Enterprise Library Configation Module and configure all application blocks available within the Enterprise Library using this UI.
The Logging Application Block
I'll show you how to configure the XML trace listener for MSVS c# using a console application.
Add a logging block to the application.
Following trace listeners can be configured:
- Custom Trace
Listener
- Database Trace
Listener
- Email Trace
Listener
- Event Log
Trace Listener
- Flat File
Trace Listener
- Message
Queuing Trace Listener
- Rolling Flat
File Trace Listener
- System
Diagnostic Trace Listener
- WMI Trace
Listener
- XML Trace Listener
XML Trace Listener
- Select Logging Target Listener => XML Trace Listener
- Select Log Message Formatter => Text Formatter
- Reference XML Listener in Categories Section
- Reference XML Listener in Special Categories Logging Errors & Warnings
- Save Configuration as a configuration file
MSVS Console Application – One Categorie [General]
- Create a new console application
- Add a reference to the Microsoft.Practices.EnterpriseLibrary.Logging assembly
- Create an app.config file and paste following configuration settings:
<?xml version="1.0" encoding="utf-8" ?>
<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="Infranet XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\Data\Listeners\trace-xml.log" traceOutputOptions="LogicalOperationStack,
DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
</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="Infranet XML Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Infranet XML Trace Listener" />
</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="Infranet XML Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
- In the main static method of the consolde application add following code:
LogEntry logEntry = new LogEntry();
logEntry.Message = "Starting up the application";
Logger.Write(logEntry);
logEntry.Message = "Ending up the application";
Logger.Write(logEntry);
The 2 LogEntries will
be written into one xml log file at the location
you configured in the InfraNet XML Trace Listener.
MSVS Console Application – Multiple Categories
- Create a new console application
- Add a reference to the Microsoft.Practices.EnterpriseLibrary.
- Logging assemblyUse the Enterprise Library Configuration UI to create a configuration file.
- Create an app.config file and paste following configuration settings:
<?xml version="1.0" encoding="utf-8" ?>
<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="Infranet General XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\Data\Listeners\trace-general.log" traceOutputOptions="LogicalOperationStack,
DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
<add name="Infranet Database XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\Data\Listeners\trace-database.log" traceOutputOptions="LogicalOperationStack,
DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
<add name="Infranet Topobase XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\Data\Listeners\trace-topobase.log" traceOutputOptions="LogicalOperationStack,
DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
</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="Infranet XML Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Infranet General XML Trace Listener" />
</listeners>
</add>
<add switchValue="All" name="Database">
<listeners>
<add name="Infranet Database XML Trace Listener" />
</listeners>
</add>
<add switchValue="All" name="Topobase">
<listeners>
<add name="Infranet Topobase XML Trace Listener" />
</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="Infranet General XML Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
- Add following code to the main method of the console application
LogEntry logEntry = new LogEntry();
logEntry.Categories = new List<string>() { "General"
};
logEntry.Message = "General Message";
Logger.Write(logEntry);
logEntry.Categories = new List<string>() { "Database"
};
logEntry.Message = "Database Message";
Logger.Write(logEntry);
logEntry.Categories = new List<string>() { "Topobase"
};
logEntry.Message = "Topobase";
Logger.Write(logEntry);
The 3 LogEntries
will be written into 3 seperate xml log files at the location configured in the
XML trace listeners.