Tenho um aplicação de console e uso o log4net para exibir os logs da aplicação, porém gostaria de salvar apenas os logs de erro no banco de dados.
Vi que tem a configuração de AdoNetAppender para gravar no banco, até então tudo ok, porém gostaria de saber como faço para que apenas logs de erros sejam gravados no banco. Preciso de alguma configuração especifica para utilizar o AdoNetAppender somente em Error?
Segue classe de configracao.
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>
Tentei de alguma forma colocar outra opção no elemento root setando o level para error e adicionando o appender-ref
para AdoNetAppender.
Quem puder ajudar agradeço.