carlosmunozledesma
7/15/2019 - 8:05 AM

log4net

Crear archivo de log. Se usa la clase log con el using log4net. También hay que poner información en el app.config

//clase log
using log4net;

namespace CargaSumasaEmpleado
{
    class log
    {
        public static void writeLog(string tipo, string msg)
        {
            ILog log = LogManager.GetLogger(typeof(log));
            log4net.Config.XmlConfigurator.Configure();
            switch (tipo)
            {
                case "info":
                    log.Info(msg);
                    break;
                case "error":
                    log.Error(msg);
                    break;
                case "warn":
                    log.Warn(msg);
                    break;
                default:
                    log.Info(msg);
                    break;
            }
            
        }

    }
}
//app.config
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\SUMASA.log" />
      <appendToFile value="true" />
      <countDirection value="1" />
      <maximumFileSize value="10MB" />
      <maxSizeRollBackups value="50" />
      <StaticLogFileName value="true" />
      <rollingStyle value="Size" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>