[英]Log4Net not writing to SQL Table
Can someone off a 2nd set of eyes? 第二组眼睛可以有人吗? I am clearly missing something.
我显然遗漏了一些东西。
I'm using Log4Net in a Web API app. 我在Web API应用程序中使用Log4Net。 Whenever I invoke a Log.Error() it executes, but nothing actually gets written to my dbo.Log table.
每当我调用一个Log.Error()时它就会执行,但实际上没有任何内容写入我的dbo.Log表。 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: Log4Net.config文件包含以下内容:
<?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. Global.asax.cs文件包含以下内容,请注意对Log4Net.Config()方法的调用,该方法指定要使用的配置。
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: 我的Controller中记录器的初始化是:
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?
有任何想法吗?
This question has been answered here . 这个问题已在这里得到解答。 The problem is your
<bufferSize>
tag, which according to the linked answer... 问题是你的
<bufferSize>
标签,根据链接的答案......
<bufferSize value="100" />
It is saying that it will keep 100 logs in memory until written into DB.
它说它将在内存中保留100个日志,直到写入数据库。 Maybe that's why you don't see anything in DB?
也许这就是为什么你在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. 将代码更改为
<bufferSize value="1" />
,这会导致log4net在保留1个内存后写入数据库。 See Apache's documentation on buffersize for more details. 有关更多详细信息,请参阅Apache有关buffersize的文档。
Have you tried running sql server profiler and doing a trace against the database in the connection string? 您是否尝试过运行sql server profiler并对连接字符串中的数据库进行跟踪? The insert statement log4net submits should show up along with values for the various parameters.
insert语句log4net提交应该与各种参数的值一起显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.