Reputation: 1385
Can someone off a 2nd set of eyes? I am clearly missing something.
I'm using Log4Net in a Web API app. Whenever I invoke a Log.Error() it executes, but nothing actually gets written to my dbo.Log table. I am running a local database. When I replaced the configuration with one that wrote to a file, the code worked perfectly.
Here is what my configuration and code looks like.
The Log4Net.config file contains the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=MUD-DAD\SQLEXPRESS;Initial Catalog=FitAchiever;Persist Security Info=True;User ID=sa;Password=asdadf334"/>
<commandText value="INSERT INTO dbo.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="DEBUG"/>
<appender-ref ref="AdoNetAppender"/>
</root>
</log4net>
</configuration>
The Global.asax.cs file contains the following, note the call to the Log4Net.Config() method which specifies which config to use.
public class WebApiApplication : System.Web.HttpApplication
{
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(WebApiApplication));
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("Log4Net.config")));
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
The initialization of the logger in my Controller is:
public class FitDashboardController : ApiController
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
IFitDashboardService fitDashboardService = new FitDashboardService();
...
And the call is:
catch (System.Exception ex)
{
log.Error("Error:" + MethodBase.GetCurrentMethod().Name + " :: " + ex.Message);
}
Again, the code works when writing to a file, but not the database. Any ideas?
Upvotes: 6
Views: 3515
Reputation: 35
Have you tried running sql server profiler and doing a trace against the database in the connection string? The insert statement log4net submits should show up along with values for the various parameters.
Upvotes: 0
Reputation: 944
This question has been answered here. The problem is your <bufferSize>
tag, which according to the linked answer...
<bufferSize value="100" />
It is saying that it will keep 100 logs in memory until written into DB. Maybe that's why you don't see anything in DB?
Change your code to say <bufferSize value="1" />
and this should cause log4net to write to your database after keeping 1 log in memory. See Apache's documentation on buffersize for more details.
Upvotes: 9