views:

301

answers:

3

hi, My objective is to log in oracle 10g using log4net through stored procedure. I have made configuration in log4net configuration xml file to use ado.net appender and use stored procedure for Logging in db. I want to log exceptions in db with parameters like error code, error message, etc. Please guide me how to pass this Exception object using C# CODE for writing in db. I have made proper configurations in xml for stored procedures.

Thanks.

Code is as follows:

      string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "log4netconfig.xml";
        FileInfo finfo = new FileInfo(logFilePath);
        log4net.Config.XmlConfigurator.ConfigureAndWatch(finfo);
        ILog logger = LogManager.GetLogger("Exception.Logging");
        log4net.ThreadContext.Properties["INNER_EXCEPTION"] = exception.InnerException.ToString();

                log4net.ThreadContext.Properties["INNER_EXCEPTION"] = string.Empty;
                log4net.ThreadContext.Properties["STACK_TRACE"] = exception.StackTrace.ToString();

                log4net.ThreadContext.Properties["STACK_TRACE"] = string.Empty;

                log4net.ThreadContext.Properties["MESSAGE"] = ((H2hException)exception).Message;
                log4net.ThreadContext.Properties["CODE"] = "err-1010";
                log4net.ThreadContext.Properties["MODULE"] = "NED.Development";
                log4net.ThreadContext.Properties["COMPONENT"] = "Component";
                log4net.ThreadContext.Properties["ADDITIONAL_MESSAGE"] = "msg";
                logger.Debug("");

  <parameter>
    <parameterName value="@p_Error_Code" />
    <dbType value="VARCHAR2" />
    <size value="16" />
    <!--<layout type="log4net.Layout.PatternLayout" value="%level" />-->
    <conversionPattern value="%property{log4net:CODE}"/>
  </parameter>
  <parameter>
    <parameterName value="@p_Error_Message" />
    <dbType value="VARCHAR2" />
    <size value="255" />
    <!--<layout type="log4net.Layout.PatternLayout" value="%logger" />-->
    <conversionPattern value="%property{log4net:MESSAGE}"/>
  </parameter>

  <parameter>
    <parameterName value="@p_Inner_Exception" />
    <dbType value="VARCHAR2" />
    <size value="4000" />
    <!--<layout type="log4net.Layout.PatternLayout" value="%thread" />-->
    <conversionPattern value="%property{log4net:INNER_EXCEPTION}"/>
  </parameter>
  <parameter>
    <parameterName value="@p_Module" />
    <dbType value="VARCHAR2" />
    <size value="225" />
    <!--<layout type="log4net.Layout.PatternLayout" value="%message" />-->
    <conversionPattern value="%property{log4net:MODULE}"/>
  </parameter>
  <parameter>
    <parameterName value="@p_Component" />
    <dbType value="VARCHAR2" />
    <size value="225" />
    <!--<layout type="log4net.Layout.ExceptionLayout" />-->
    <conversionPattern value="%property{log4net:COMPONENT}"/>
  </parameter>
  <parameter>
    <parameterName value="@p_Stack_Trace " />
    <dbType value="VARCHAR2" />
    <size value="4000" />
    <!--<layout type="log4net.Layout.PatternLayout"/>-->
    <conversionPattern value="%property{log4net:STACK_TRACE}"/>
  </parameter>
  <parameter>
    <parameterName value=" @p_Additional_Message" />
    <dbType value="VARCHAR2" />
    <size value="4000" />
    <!--<layout type="log4net.Layout.ExceptionLayout" />-->
    <conversionPattern value="%property{log4net:ADDITIONAL_MESSAGE}"/>
  </parameter>
</appender>

A: 

Have you looked at this already http://logging.apache.org/log4net/release/config-examples.html

Ashwani Roy
A: 

I think your config is slightly off... Try this

Note that the ADO appender buffers inputs (I dont know to what degree) but this means that the entry will not appear in the database immediately.

As johnathon mentions, given that you have not posted the full configuration, we cannot check any filters, connection strings or Stored proc setup either.

Your actual code looks fine. However I would put some text in the

logger.Debug("");

line, just in case the appender is throwing out logs that have no data...

Hope this Helps somewhat...

Overflow
+1  A: 

Log4net buffers SQL commands. You can set the buffer like this:

<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="128" />
</appender>
Petr Kozelek