locked
how to log exceptions and trace in one text format file? RRS feed

  • Question

  • User-174184358 posted
    I could log the below exception to log file. try { throw new Exception(); } catch (Exception ex) { Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "ExceptionPolicy"); } ---------------------------------------------- i could trace the application in trace.log file. using (new Tracer("Trace")) { //my logic } ---------------------------- below is my 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=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </configSections> <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> <listeners> <add fileName="trace.log" header="----------------------------------------" footer="----------------------------------------" formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="FlatFile TraceListener" /> <add fileName="trace.log" header="----------------------------------------" footer="----------------------------------------" formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="FlatFile exTraceListener1" /> <add source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Formatted EventLog TraceListener" /> </listeners> <formatters> <add 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} )}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Text Formatter" /> </formatters> <categorySources> <add switchValue="All" name="exc"> <listeners> <add name="FlatFile exTraceListener1" /> </listeners> </add> <add switchValue="All" name="General"> <listeners> <add name="Formatted EventLog TraceListener" /> </listeners> </add> <add switchValue="ActivityTracing" name="Trace"> <listeners> <add name="FlatFile TraceListener" /> </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="Formatted EventLog TraceListener" /> <add name="FlatFile exTraceListener1" /> </listeners> </errors> </specialSources> </loggingConfiguration> <exceptionHandling> <exceptionPolicies> <add name="ExceptionPolicy"> <exceptionTypes> <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None" name="Exception"> <exceptionHandlers> <add logCategory="exc" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" priority="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Logging Handler" /> </exceptionHandlers> </add> </exceptionTypes> </add> </exceptionPolicies> </exceptionHandling> </configuration> now the thing is , i want to log any type of exception and also application trace in one single xyz.log file. how do i do this? i have been trying this since couple of days but .... please help me.
    Friday, September 26, 2008 9:17 AM

All replies

  • User122281327 posted

     Hi!

    First catch the exception like this

    catch (Exception ex) {

    //sXML = ex.Message.ToString();

    Logging("origen", "type desc", ex.ToString()); return sXML; throw;

    }

     and then log it like this

        private void Logging(string op, string par, string texto)
        {
            /*
            //TextWriter tw = new StreamWriter("d:\\inetpub\\wwwroot\\usuarios\\LogEBS.txt", true, Encoding.Default, 1024);
            TextWriter tw = new StreamWriter(pathLog, true, Encoding.Default, 1024);
           
            tw.WriteLine(op +"," +par+ "," + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond + "," + texto);
        

            // close the stream
            tw.Close();       
             */
        }
     

     

    GL

     

    Friday, September 26, 2008 9:42 AM
  • User-1061804453 posted

    Hopefully I read your question properly, but it's awfully unclear... Rule of thumb:  if the program throws an exception, catch it somewhere.  When you catch it, log it.  For example:

     

    ' Just /pretend/ that I've declared all the necessary variables.
    Public Class MailerObj
    Public Sub Send()
    Try
    SmtpClient.Send(from, to, subject, body)
    Catch ex As Exception
    Throw New Exception(ex.Message)
    End Try
    End Sub
    End Class


    ' This class calls our object
    Public Sub SendMail(ByVal from, ByVal to, ByVal subject, ByVal body)
    Try
    Dim
    mailer As New MailerObj
    mailer.Send(from, to, subject, body)
    Catch ex As Exception
    ' 1. Write entry to log file xyz.log
    ' 2. Show error message

    End Try
    End Sub

     

    First and foremost, we know that SmtpObject.Send() returns a number of exceptions, but we don't care what they are.  So, we just throw a new Exception altogether.  From that, we know our MailerObj throws an Exception if Send() fails, so we place any call to MailerObj.Send() in a try/catch block, in which we write the error to the log file...  The hierarchy looks like this:

    • SmtpClient.Send() throws:
      • ArgumentNullException
      • ArgumentException
      • InvalidOperationException
      • ObjectDisposedException
      • SmtpException
      • SmtpFailedRecipientsException.
    • MailerObj.Send() catches SmtpClient.Send exceptions (again, we don't care what type).  On catch, throws:
      • Exception
    • Sub SendMail() catches MailerObj.Send() exceptions.  On catch, logs error to xyz.log.

     

     

    Friday, September 26, 2008 9:46 AM