How to use Log4net Adonetappender only at Error level

Asked

Viewed 67 times

0

I have a console application and use log4net to display the application logs, however I would like to save only the error logs in the database.

I saw that you have the configuration of Adonetappender to record in the bank, until then everything ok, but I would like to know how to make only error logs are recorded in the bank. I need some specific configuration to use Adonetappender only in Error?

Following class of confession.

public class Log
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public Log()
        {

        }

        public static void Debug(object messageOrEntity)
        {
            log.Debug(messageOrEntity);
        }

        public static void Debug(object messageOrEntity, Exception ex)
        {
            log.Debug(messageOrEntity);
        }

        public static void DebugFormat(string format, params object[] args)
        {
            log.DebugFormat(format, args);
        }

        public static void Information(object messageOrEntity)
        {
            log.Info(messageOrEntity);
        }

        public static void Information(object messageOrEntity, Exception ex)
        {
            log.Info(messageOrEntity, ex);
        }


        public static void InformationFormat(string format, params object[] args)
        {
            log.InfoFormat(format, args);
        }

        public static void Warning(object messageOrEntity)
        {
            log.Warn(messageOrEntity);
        }

        public static void Warning(object messageOrEntity, Exception ex)
        {
            log.Warn(messageOrEntity, ex);
        }

        public static void WarningFormat(string format, params object[] args)
        {
            log.WarnFormat(format, args);
        }

        public static void Error(object messageOrEntity)
        {
            log.Error(messageOrEntity);
        }

        public static void Error(object messageOrEntity, Exception ex)
        {
            log.Error(messageOrEntity, ex);
        }

        public static void ErrorFormat(string format, params object[] args)
        {
            log.ErrorFormat(format, args);
        }

        public static void Fatal(object messageOrEntity)
        {
            log.Fatal(messageOrEntity);
        }

        public static void Fatal(object messageOrEntity, Exception ex)
        {
            log.Fatal(messageOrEntity, ex);
        }

        public static void FatalFormat(string format, params object[] args)
        {
            log.FatalFormat(format, args);
        }


    }

Appconfig


<log4net>
    <appender name="Console" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="INFO" />
        <foreColor value="Green"/>
      </mapping>

      <mapping>
        <level value="DEBUG" />
        <foreColor value="Cyan,HighIntensity"/>
      </mapping>

      <mapping>
        <level value="WARN" />
        <foreColor value="Yellow,HighIntensity"/>
      </mapping>

      <mapping>
        <level value="ERROR" />
        <foreColor value="Red,HighIntensity"/>
      </mapping>

      <mapping>
        <level value="FATAL" />
        <foreColor value="Purple,HighIntensity"/>
      </mapping>

      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level: %message%newline" />
      </layout>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="4" />
      <maximumFileSize value="150KB" />
      <staticLogFileName value="true" />
      <param name="File" value="C:\Users\M215319\Documents\ProjetoBradescoPGP\BradescoPGPConsole\BradescoPGP.Console\Logs\BradescoPGPService.log" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline" />
      </layout>
    </appender>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
    <root>
      <level value="All" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="Console" />
    </root>
  </log4net>

I tried somehow to put another option in the root element by setting the level to error and adding the appender-ref to Adonetappender.

Anyone who can help, thank you.

2 answers

1

  • OK I will try to use and return if it works. Thank you

  • I used this setting until it worked out not yet got as I wanted it. This way the error logs, fatal and some others were also captured.

1


In case someone also needs this information I was able to solve the problem using the Levelmatchfilter to configure the logger level.

<filter type="log4net.Filter.LevelMatchFilter">
    <acceptOnMatch value="true" />
    <levelToMatch  value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.