[英]No appenders instantiated when running locally
I have a web application which uses log4net. 我有一个使用log4net的Web应用程序。 My web application is deployed to Azure, and works there perfectly. 我的Web应用程序已部署到Azure,并且可以在其中完美运行。 However when I run it locally through VS2013 (update 4), log4net doesn't instantiate the AdoNetAppender
. 但是,当我通过VS2013在本地运行它(更新4)时,log4net不会实例化AdoNetAppender
。
I checked for exception by breaking on all thrown exceptions, nothing was thrown, I activated the internal log4net debugging and nothing exceptional showed in the debug output. 我通过打破所有引发的异常来检查异常,没有引发任何异常,我激活了内部log4net调试,并且调试输出中没有异常显示。
I also added <trust level="Full"/>
to my web.config
, nothing... 我还添加了<trust level="Full"/>
到我的web.config
,什么也没有...
I'm stumped, again, it works on Azure, but not locally. 再次让我感到困惑的是,它可以在Azure上运行,但不能在本地运行。 Any ideas? 有任何想法吗? Thanks! 谢谢!
EDIT Here's my web.config
(relevant lines only) 编辑这是我的web.config
(仅相关行)
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=**********;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
<log4net debug="true">
<appender name="ADONetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionStringName value="DefaultConnection" />
<commandText value="INSERT INTO Log ([StoreId],[Date],[Thread],[Level],[Logger],[Message],[Exception],[User]) VALUES (@store, @log_date, @thread, @log_level, @logger, @message, @exception, @user)" />
<parameter>...</parameter>
...
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<immediateFlush value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
<appender-ref ref="DebugAppender" />
</root>
</log4net>
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
<add key="log4net.Config" value="log4net.simple.config"/>
<add key="log4net.Config.Watch" value="True"/>
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" maxRequestLength="1048576" />
<customErrors mode="Off" />
<trust level="Full"/>
</system.web>
</configuration>
UPDATE 2 更新2
I discovered that although log4net
is configured properly, calling LogManager.GetLogger("...")
returns a logger with no appenders. 我发现尽管log4net
配置正确,但调用LogManager.GetLogger("...")
返回没有附加程序的记录器。 However if I inspect the hierarchy further I can see my AdoNetAppender
in there, and also when XmlConfigurator.Configure()
runs it outputs the following log4net: Adding appender named [ADONetAppender] to logger [root].
但是,如果我进一步检查层次结构,则可以在其中看到我的AdoNetAppender
,并且当XmlConfigurator.Configure()
运行时,它还会输出以下log4net: Adding appender named [ADONetAppender] to logger [root].
... ...
So the problem boils down to (and this happens locally only, not on production) that the object returned from LogManager.GetLogger(...)
doesn't contain any appenders. 因此,问题归结为(而且这仅在本地发生,而不是在生产中发生)从LogManager.GetLogger(...)
返回的对象不包含任何附加程序。
SOLVED 解决了
See accepted answer below. 请参阅下面的接受的答案。 It turns out all I was really missing was a call to XmlConfigurator.Configure()
in my Application_Start
method. 事实证明,我真正缺少的只是在Application_Start
方法中对XmlConfigurator.Configure()
的调用。 All the other 'evidence' I provided is normal behavior, and the reason I wasn't seeing any log messages locally was a slight unintentional difference between the log table schema between my local db and the production one. 我提供的所有其他“证据”都是正常行为,而我在本地未看到任何日志消息的原因是本地数据库与生产数据库之间的日志表架构之间存在一些无意的差异。
You can configure log4net in the Global.asax: 您可以在Global.asax中配置log4net:
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
XmlConfigurator.Configure();
This works for azure and local. 这适用于天蓝色和本地。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.