L A

L A

  • NA
  • 170
  • 168.5k

Exception Handling & using Microsoft Enterprise Library 6.0

May 25 2018 6:48 PM
Hello,
 
I'm working on ASP.Net MVC trying to implement Logging & Tracing. I've followed this (https://www.c-sharpcorner.com/article/exception-handling-block-in-microsoft-enterprise-library-6-0/)article on C# corner and tried to implement but couldn't get required output as shown by original writer (I might have missed something). I don't want to log in windows event logger, so i tweaked web.config.
 
Added required Microsoft Enterprise Library 6.0 refrences
 
 
Changed web.config file
  1. <configSections>    
  2.    <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" />    
  3.    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />    
  4. </configSections>    
  5. <loggingConfiguration name="" tracingEnabled="false" defaultCategory="General" logWarningsWhenNoCategoriesMatch="false">    
  6.    <listeners>    
  7.       <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:\Logs\{timestamp(local)}RollingFlatFile.log" header="----------------------------------" footer="----------------------------------" formatter="Text Formatter" rollInterval="Day" filter="All" rollFileExistsBehavior="Increment" traceOutputOptions="LogicalOperationStack" rollSizeKB="1024" />    
  8.    </listeners>    
  9.    <formatters>    
  10.       <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}{newline}Message: {message}{newline}" name="Text Formatter" />    
  11.    </formatters>    
  12.    <categorySources>    
  13.       <add switchValue="All" autoFlush="true" name="General">    
  14.          <listeners>    
  15.             <add name="Rolling Flat File Trace Listener" />    
  16.          </listeners>    
  17.       </add>    
  18.    </categorySources>    
  19.    <specialSources>    
  20.       <allEvents switchValue="All" name="All Events">    
  21.          <listeners>    
  22.             <add name="Rolling Flat File Trace Listener" />    
  23.          </listeners>    
  24.       </allEvents>    
  25.       <notProcessed switchValue="All" name="Unprocessed Category">    
  26.          <listeners>    
  27.             <add name="Rolling Flat File Trace Listener" />    
  28.          </listeners>    
  29.       </notProcessed>    
  30.       <errors switchValue="All" name="Logging Errors & Warnings">    
  31.          <listeners>    
  32.             <add name="Rolling Flat File Trace Listener" />    
  33.          </listeners>    
  34.       </errors>    
  35.    </specialSources>    
  36. </loggingConfiguration>    
  37. <exceptionHandling>    
  38.    <exceptionPolicies>    
  39.       <add name="ExceptionHandling">    
  40.          <exceptionTypes>    
  41.             <add    
  42.                name="Base Exception Handler"    
  43.                type="System.Exception,mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"    
  44.                postHandlingAction="ThrowNewException">    
  45.                <exceptionHandlers>    
  46.                   <add    
  47.                      name="Base Wrap Handler"    
  48.                      type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"    
  49.                      exceptionMessage="Exception occurred in application."    
  50.                      wrapExceptionType="System.ApplicationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />    
  51.                   <add    
  52.                      name="Logging Exception Handler"    
  53.                      type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"    
  54.                      logCategory="General"    
  55.                      eventId="9000"    
  56.                      severity="Error"    
  57.                      title="ExceptionHandlingBlock"    
  58.                      formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"    
  59.                      priority="5" />    
  60.                </exceptionHandlers>    
  61.             </add>    
  62.          </exceptionTypes>    
  63.       </add>    
  64.    </exceptionPolicies>    
  65. </exceptionHandling>  
Added class.cs file for logging
  1. public class Logger    
  2. {    
  3.     private static LogWriter logWriter;    
  4.     private static readonly ExceptionPolicyFactory exceptionPolicyFactory;    
  5.     private static readonly ExceptionManager exceptionManager;    
  6.     static Logger()    
  7.     {    
  8.         logWriter = new LogWriterFactory().Create();    
  9.         Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetLogWriter(logWriter, true);    
  10.         exceptionPolicyFactory = new ExceptionPolicyFactory();    
  11.         exceptionManager = exceptionPolicyFactory.CreateManager();    
  12.     }    
  13.     
  14.     public LogWriter LogWriter    
  15.     {    
  16.         get { return logWriter; }    
  17.     }    
  18.     
  19.     public static void HandleException(Exception exception)    
  20.     {    
  21.         logWriter.Write(exception, "ExceptionHandling");    
  22.     }    
  23. }   
Calling 'Logger.HandleException(ex)' in catch block, which generates logfile.
 
Generated data in log file
----------------------------------
Timestamp: 5/25/2018 3:18:29 PM
Message: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.Write(Object message, IEnumerable`1 categories, Int32 priority, Int32 eventId, TraceEventType severity, String title, IDictionary`2 properties)
at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.Write(Object message, String category)
at Loggingtool.Logger.HandleException(Exception exception) in C:\...\src\Logger.cs:line 28
at Loggingtool.Controllers.JobController.Index() in C:\...\Controllers\JobController.cs:line 19
----------------------------------
 
I really have no idea about these web.config setting, because these setting are crucial to achieve stack-trace, parameters-passed, inner exception, inner-exception message, calling method, etc.,
 
Please help me to correct my mistakes. Suggestions for better-way to handle these stuff is always appreciated.Thanks in advance.