0

Background: We are using AdoNetAppender to log into an Oracle Database. There are 5 unit tests all of which log dummy error messages into the same table. The Logger instance is resolved by Unity and the lifetime is PerThreadLifetimeManager.

The code which logs into the database is as below

public void Error(LogInformation message, Exception exception)
{
    mapLogInformationToContext(message);
    Task.Factory.StartNew(() => Logger.Log(_stackBoundary, Level.Error, message, exception));
 }`

And the log information is mapped to ThreadContext as shown below:

private static void mapLogInformationToContext(LogInformation message)
{
    ThreadContext.Properties["LogUserName"] = message.LogUserName;
    ThreadContext.Properties["LogMachineName"] = message.LogMachineName;
    ThreadContext.Properties["LogType"] = message.LogType;
    ThreadContext.Properties["LogCreateDate"] = message.LogCreateDate == default(DateTime) ? DateTime.Now : message.LogCreateDate;
    if (message.LogPropertiesXml != null)
    {
        ThreadContext.Properties["LogPropertiesXml"] = message.LogPropertiesXml.Trim();
    }
}

The problem which we are facing is with LogPropertiesXml. Even a well formatted Xml fails to log into the database with the Task.Factory.StartNew. If Tasking is eliminated, all 5 unit tests work. Otherwise only 1-2 tests randomly log into the database and others fail with this error

ORA-19202: Error occurred in XML processing LPX-00210: expected '<' instead of '(' Error at line 1 at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck, Int32 isRecoverable) at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, Boolean bCheck) at Oracle.DataAccess.Client.OracleCommand.ExecuteNonQuery() at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events) at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)

What am I doing wrong here?

stuartd
  • 70,509
  • 14
  • 132
  • 163
Rahul
  • 319
  • 3
  • 6
  • Can you run a profiler to see what's being sent to the database, specifically what is in the message when the xml starts with a '(' ? – stuartd Jul 16 '15 at 21:05
  • LogSource Framework.Logging.UnitTests.LogInfoDebugTests, Framework.Logging.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null – Rahul Jul 16 '15 at 21:28
  • And that's a message that failed and caused the error? – stuartd Jul 16 '15 at 22:03
  • That's not valid XML because of the semicolons between elements. – stuartd Jul 16 '15 at 22:08
  • Hey Stuart, The semicolons were added here in formatting. The actual Xml does not have semi colons. Second thing is, this Xml is logged sometimes and sometimes not. The issues I am facing are with Threads/Tasks. – Rahul Jul 17 '15 at 14:03
  • 1
    You are adding properties to the current thread - i.e. the ThreadContext - but then passing your logging task onto the thread pool for scheduling. – stuartd Jul 17 '15 at 14:34
  • Worked for me Thanks. – Rahul Aug 16 '17 at 14:08

0 Answers0