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
- <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="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
- </configSections>
- <loggingConfiguration name="" tracingEnabled="false" defaultCategory="General" logWarningsWhenNoCategoriesMatch="false">
- <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:\Logs\{timestamp(local)}RollingFlatFile.log" header="----------------------------------" footer="----------------------------------" formatter="Text Formatter" rollInterval="Day" filter="All" rollFileExistsBehavior="Increment" traceOutputOptions="LogicalOperationStack" rollSizeKB="1024" />
- </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(local)}{newline}Message: {message}{newline}" name="Text Formatter" />
- </formatters>
- <categorySources>
- <add switchValue="All" autoFlush="true" name="General">
- <listeners>
- <add name="Rolling Flat File Trace Listener" />
- </listeners>
- </add>
- </categorySources>
- <specialSources>
- <allEvents switchValue="All" name="All Events">
- <listeners>
- <add name="Rolling Flat File Trace Listener" />
- </listeners>
- </allEvents>
- <notProcessed switchValue="All" name="Unprocessed Category">
- <listeners>
- <add name="Rolling Flat File Trace Listener" />
- </listeners>
- </notProcessed>
- <errors switchValue="All" name="Logging Errors & Warnings">
- <listeners>
- <add name="Rolling Flat File Trace Listener" />
- </listeners>
- </errors>
- </specialSources>
- </loggingConfiguration>
- <exceptionHandling>
- <exceptionPolicies>
- <add name="ExceptionHandling">
- <exceptionTypes>
- <add
- name="Base Exception Handler"
- type="System.Exception,mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
- postHandlingAction="ThrowNewException">
- <exceptionHandlers>
- <add
- name="Base Wrap Handler"
- type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
- exceptionMessage="Exception occurred in application."
- wrapExceptionType="System.ApplicationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
- <add
- name="Logging Exception Handler"
- type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- logCategory="General"
- eventId="9000"
- severity="Error"
- title="ExceptionHandlingBlock"
- formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- priority="5" />
- </exceptionHandlers>
- </add>
- </exceptionTypes>
- </add>
- </exceptionPolicies>
- </exceptionHandling>
Added class.cs file for logging
- public class Logger
- {
- private static LogWriter logWriter;
- private static readonly ExceptionPolicyFactory exceptionPolicyFactory;
- private static readonly ExceptionManager exceptionManager;
- static Logger()
- {
- logWriter = new LogWriterFactory().Create();
- Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetLogWriter(logWriter, true);
- exceptionPolicyFactory = new ExceptionPolicyFactory();
- exceptionManager = exceptionPolicyFactory.CreateManager();
- }
-
- public LogWriter LogWriter
- {
- get { return logWriter; }
- }
-
- public static void HandleException(Exception exception)
- {
- logWriter.Write(exception, "ExceptionHandling");
- }
- }
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.