First Download Microsoft Enterprise Library 5.0
http://www.microsoft.com/en-us/download/details.aspx?id=15104
Step 2:
Install Enterprise Library 5.0 in your system.
Step 3:
Create a test project named TestEnterpriseLibrary
Step 4:
Add following reference to your project
Microsoft.Practices.EnterpriseLibrary.Logging
Step 5:
Add App.config file in your project
Step 6:
Right click on app file and click on Edit Enterprise Library V5 Configuration
Step 7:
Go to Block then Add Logging Settings
Step 8:
Expand the Logging Settings and do the following
Step 9:
Step 10:
Step 11:
Step 12:
Add Logging Filter
If All Logging Enabled is set to true, log will write on file otherwise no log will maintain.
Step 13:
using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace TestEnterpriseLibrary
{
//********************************************************************************************************************************
// Class:Log
//********************************************************************************************************************************
// M O D I F I C A T I O N M I L E S T O N E S
// -------------------------------------------------------------------------------------------------------------------------------
// Date | Created By | Description
// --------+-------------------+--------------------------------------------------------------------------------------------------
// Apr 02, 2013 | Kaushik Kumar Mistry | This Class is used to call the Interaction logic for Log
//*********************************************************************************************************************************
/// <summary>
/// This Class is used to call the Interaction logic for Log
/// </summary>
/// <Developer>Kaushik Kumar Mistry</Developer>
/// <DateCreated>02 April 2012</DateCreated>
public static class Log
{
public static void WriteException(Exception ex)
{
if (Logger.IsLoggingEnabled())
{
StackTrace stackTrace = new StackTrace(ex, true); // Get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // Get method calls (frames)
// write call stack method names
foreach (StackFrame stackFrame in stackFrames)
{
LogEntry loggerEntry = new LogEntry();
loggerEntry.TimeStamp = DateTime.Now;
loggerEntry.Message = String.Format("File Name: {0} Method: {1} Line: {2} Column: {3} Exception:{4}", stackFrame.GetFileName(), stackFrame.GetMethod().Name, stackFrame.GetFileColumnNumber(), stackFrame.GetFileLineNumber(), ex.ToString());
Logger.Write(loggerEntry);
}
}
}
public static void WriteException(Exception ex,String title)
{
if (Logger.IsLoggingEnabled())
{
StackTrace stackTrace = new StackTrace(ex, true); // Get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // Get method calls (frames)
// write call stack method names
foreach (StackFrame stackFrame in stackFrames)
{
LogEntry loggerEntry = new LogEntry();
loggerEntry.Title = title;
loggerEntry.TimeStamp = DateTime.Now;
loggerEntry.Message = String.Format("File Name: {0} Method: {1} Line: {2} Column: {3} Exception:{4}", stackFrame.GetFileName(), stackFrame.GetMethod().Name, stackFrame.GetFileColumnNumber(), stackFrame.GetFileLineNumber(), ex.ToString());
Logger.Write(loggerEntry);
}
}
}
}
}
******************************************************************
using System;
namespace TestEnterpriseLibrary
{
class Program
{
static void Main(string[] args)
{
try
{
int divideBy = 0;
int x = 1 / divideBy;
}
catch (Exception ex)
{
Log.WriteException(ex);
}
Console.Read();
}
}
}
******************************************************************
<?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="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="" machineName="." traceOutputOptions="None" />
<add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\trace.log" formatter="Text Formatter" traceOutputOptions="DateTime" />
</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" />
</logFilters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="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 & Warnings">
<listeners>
<add name="Event Log Listener" />
<add name="Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
Go to Block then Add Logging Settings
Step 8:
Expand the Logging Settings and do the following
Step 9:
Step 10:
Step 11:
Step 12:
Add Logging Filter
If All Logging Enabled is set to true, log will write on file otherwise no log will maintain.
Step 13:
using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace TestEnterpriseLibrary
{
//********************************************************************************************************************************
// Class:Log
//********************************************************************************************************************************
// M O D I F I C A T I O N M I L E S T O N E S
// -------------------------------------------------------------------------------------------------------------------------------
// Date | Created By | Description
// --------+-------------------+--------------------------------------------------------------------------------------------------
// Apr 02, 2013 | Kaushik Kumar Mistry | This Class is used to call the Interaction logic for Log
//*********************************************************************************************************************************
/// <summary>
/// This Class is used to call the Interaction logic for Log
/// </summary>
/// <Developer>Kaushik Kumar Mistry</Developer>
/// <DateCreated>02 April 2012</DateCreated>
public static class Log
{
public static void WriteException(Exception ex)
{
if (Logger.IsLoggingEnabled())
{
StackTrace stackTrace = new StackTrace(ex, true); // Get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // Get method calls (frames)
// write call stack method names
foreach (StackFrame stackFrame in stackFrames)
{
LogEntry loggerEntry = new LogEntry();
loggerEntry.TimeStamp = DateTime.Now;
loggerEntry.Message = String.Format("File Name: {0} Method: {1} Line: {2} Column: {3} Exception:{4}", stackFrame.GetFileName(), stackFrame.GetMethod().Name, stackFrame.GetFileColumnNumber(), stackFrame.GetFileLineNumber(), ex.ToString());
Logger.Write(loggerEntry);
}
}
}
public static void WriteException(Exception ex,String title)
{
if (Logger.IsLoggingEnabled())
{
StackTrace stackTrace = new StackTrace(ex, true); // Get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // Get method calls (frames)
// write call stack method names
foreach (StackFrame stackFrame in stackFrames)
{
LogEntry loggerEntry = new LogEntry();
loggerEntry.Title = title;
loggerEntry.TimeStamp = DateTime.Now;
loggerEntry.Message = String.Format("File Name: {0} Method: {1} Line: {2} Column: {3} Exception:{4}", stackFrame.GetFileName(), stackFrame.GetMethod().Name, stackFrame.GetFileColumnNumber(), stackFrame.GetFileLineNumber(), ex.ToString());
Logger.Write(loggerEntry);
}
}
}
}
}
******************************************************************
using System;
namespace TestEnterpriseLibrary
{
class Program
{
static void Main(string[] args)
{
try
{
int divideBy = 0;
int x = 1 / divideBy;
}
catch (Exception ex)
{
Log.WriteException(ex);
}
Console.Read();
}
}
}
******************************************************************
<?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="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="" machineName="." traceOutputOptions="None" />
<add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\trace.log" formatter="Text Formatter" traceOutputOptions="DateTime" />
</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" />
</logFilters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="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 & Warnings">
<listeners>
<add name="Event Log Listener" />
<add name="Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
No comments:
Post a Comment