0
I am using log4Net as a Log framework for a Winforms APP.
public class Logger
{
private ILog _logErro;
private const string LogErro = "LoggerErro";
public Logger()
{
Configure();
}
private void Configure()
{
_logErro = LogManager.GetLogger(LogErro);
}
public void Erro(string mensagem)
{
_logErro.Error(mensagem);
}
}
In assemblyinfo.Cs I added
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.Config", Watch = true)]
The log4net.Config file has the following content:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=(localdb)\ProjectsV13;Initial Catalog=XXXXX;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" />
<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="AdoNetAppender" />
</root>
</log4net>
When I execute the command:
Logger.Erro("Mensagem de erro");
No error message appears, but it also does not write to the database.
Have you tried using protected in the variable declaration instead of private? I use log4net here, but I present debugs and error messages playing this way:
protected static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
– Daniel Nicodemos
Daniel, I was using SQL EXPRESS to record them so it didn’t work when I switched to SQL SERVER worked. I will test your suggestion.
– b3r3ch1t