Clean And Simple Way To Log Entire Contents/Properties Of C# Objects

        /// <summary>
        /// Objects to XML.
        /// </summary>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public static string ObjectToXml(object output)
        {
            string objectAsXmlString;

            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(output.GetType());
            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                try
                {
                    xs.Serialize(sw, output);
                    objectAsXmlString = sw.ToString();
                }
                catch (Exception ex)
                {
                    objectAsXmlString = ex.ToString();
                }
            }

            return objectAsXmlString;
        }

How To Easily Add Log4Net To Your .NET Project

  1. Download Log4Net binaries –>  http://logging.apache.org/log4net/download_log4net.cgi
  2. Add Reference to Log4Net.dll in your project
  3. Create new “Logs” folder and ensure Web App/Site has Write privileges to this directory
  4. Add the following to Global.asmx
     
        /// &lt;summary&gt;
        /// Gets the application log4net log instance.
        /// &lt;/summary&gt;
        /// &lt;value&gt;The log.&lt;/value&gt;
        public ILog log
        {
            get { return LogManager.GetLogger(&quot;AppLogger&quot;); }
        }
    
        void Application_Start(object sender, EventArgs e) 
        {
            // Code that runs on application startup
            log4net.Config.XmlConfigurator.Configure();        
    
        }
        
    
  5. Add Log4Net config section and declaration into web.config/app.config
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>
      <appSettings>   
      </appSettings>
      <log4net>
        <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="Logs/application.log"/>
          <appendToFile value="true"/>
          <rollingStyle value="Composite"/>
          <datePattern value="yyyyMMdd"/>
          <maxSizeRollBackups value="10"/>
          <maximumFileSize value="5MB"/>
          <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%n DATE: %date{MM/dd/yyyy HH:mm:ss} %n THREAD: [%thread] %n LEVEL: %-5level %n USER ID: %identity %n CLASS: %type{1} %n METHOD: %M %n LOCATION: %location %n MESSAGE: %message %n "/>
          </layout>
        </appender>
        <root>
          <level value="DEBUG"/>
          <appender-ref ref="LogFileAppender"/>
        </root>
      </log4net>
    </configuration>
    
  6. Add private static readonly property referencing ILog
      class Program
        {       
            /// &lt;summary&gt;
            /// Logging
            /// &lt;/summary&gt;
            //private readonly ILog Logger = DecisionLogManager.GetLogger(typeof(UC4Request));    
          private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    
  7. Call Error method to log error
     
                catch (Exception ex)
                {
                    // Log to local log file
                    log.Error("Error", ex);
                }
    
  8. You can also call other methods (Info) to output debug information
     
             log.Info(responseOutput +
                             Environment.NewLine +
                             Base.HelperFunctions.ObjectToString(keywordClientReply, DisplayMode.HTML)
                             );