Link to home
Start Free TrialLog in
Avatar of omegalove
omegalove

asked on

using log4net in debug mode only writes to db when stopping debug.

How can I fix so when in debug mode log4net writes to the db immediatel?

 <log4net>
    <appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <connectionString value="Data Source=WM7-1111-MATHC;Initial Catalog= ;Integrated Security=True;" providerName="System.Data.SqlClient"/>
      <commandText value="INSERT INTO OWU_Log4Net ([Date],[Level],[Logger],[User],[Message],[Exception],[Thread]) VALUES (@log_date, @log_level, @logger, @user, @message, @exception, @thread)"/>
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
      </parameter>
      <parameter>
        <parameterName value="@log_level"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%p"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%c"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@user"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%X{user}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message"/>
        <dbType value="String"/>
        <size value="4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%m"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception"/>
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="log4net.Layout.ExceptionLayout"/>
      </parameter>
      <parameter>
        <parameterName value="@thread"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%t"/>
        </layout>
      </parameter>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="AdoNetAppender_SqlServer"/>
    </root>
  </log4net>
using System;
using System.Collections.Generic;
using System.Text;

using log4net;

public static class Log
{
   private const string LOG_REPOSITORY = "Default"; // this should likely be set in the web config.
   private static ILog m_log;

   public static void Init()
   {
      log4net.Config.XmlConfigurator.Configure();
   }

   public static void Write(string message, LogMessageType messageType)
   {
      DoLog(message, messageType, null, Type.GetType("System.Object"));
   }

   public static void Write(string message, LogMessageType messageType, Type type)
   {
      DoLog(message, messageType, null, type);
   }

   public static void Write(string message, LogMessageType messageType, Exception ex)
   {
      DoLog(message, messageType, ex, Type.GetType("System.Object"));
   }

   public static void Write(string message, LogMessageType messageType, Exception ex,
      Type type)
   {
      DoLog(message, messageType, ex, type);
   }

   public static void Assert(bool condition, string message)
   {
      Assert(condition, message, Type.GetType("System.Object"));
   }

   public static void Assert(bool condition, string message, Type type)
   {
      if (condition == false)
         Write(message, LogMessageType.Info);
   }

   private static void DoLog(string message, LogMessageType messageType, Exception ex,
      Type type)
   {
      m_log = LogManager.GetLogger(type);

      switch (messageType)
      {
         case LogMessageType.Debug:
            Log.m_log.Debug(message, ex);
            break;

         case LogMessageType.Info:
            Log.m_log.Info(message, ex);
            break;

         case LogMessageType.Warn:
            Log.m_log.Warn(message, ex);
            break;

         case LogMessageType.Error:
            Log.m_log.Error(message, ex);
            break;

         case LogMessageType.Fatal:
            Log.m_log.Fatal(message, ex);
            break;
      }
   }

   public enum LogMessageType
   {
      Debug,
      Info,
      Warn,
      Error,
      Fatal
   }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nmarun
nmarun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of omegalove
omegalove

ASKER

awesome