- Add reference to EL Logging libraries through nuget.
- Download Enterprise Library 6 separately. It will contain a powershell script
- Run the .\install-packages.ps1 script. It will download the EL binaries and Configuration Editor Tool. The configuration Editor tool is useful in writing configuration for Logging (web.config) as it can be very verbose while writing manually.
- The extracted contents would also contain a SQL script file for creation of “Logging” database and its objects.
The following the sample
lines of code for a Console App.
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(configurationSource));
Logger.SetLogWriter(logWriterFactory.Create());
Logger.Write("Test EL -
DBOnly","DBOnly");
Logger.Write("Test EL -
TextOnly", "TextOnly");
Logger.Write("Test EL - General", "General");
The following
lines are taken from web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="Logging" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial
Catalog=Logging;User Id=sa;Password=Pass@word1" />
</connectionStrings>
<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:\temp123\ELTestLog.txt" footer="---------------------------" formatter="Text Formatter" header="---------------------------" rollFileExistsBehavior="Increment"
rollSizeKB="10" timeStampPattern="yyyy-MM-dd hh:mm:ss" maxArchivedFiles="7" traceOutputOptions="Timestamp, Callstack" filter="All"/>
<add databaseInstanceName="Logging" writeLogStoredProcName="WriteLog" addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Database Trace Listener" />
</listeners>
<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}
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}
)}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="TextOnly">
<listeners>
<add name="Rolling Flat File Trace
Listener" />
</listeners>
</add>
<add switchValue="All" name="DBOnly">
<listeners>
<add name="Database Trace Listener" />
</listeners>
</add>
<add switchValue="All" name="General">
<listeners>
<add name="Rolling Flat File Trace
Listener" />
<add name="Database 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="Rolling Flat File Trace
Listener" />
<add name="Database Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<dataConfiguration defaultDatabase="Logging" />
</configuration>
The configuration above shows
there are 2 listeners (for file and database) and there are 3 categories. It also
shows mapping between listeners and categories.
While logging you can specify
the categorySource to make it log just in text, or just in database or both.
No comments:
Post a Comment