简体   繁体   English

在本地运行时,没有实例化任何附加程序

[英]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.

相关问题 如何在本地选择默认文化 - (以及在 Azure 上运行时为什么不选择) - How is a default culture selected locally - (and why not when running on Azure) 在本地运行 Azure Functions - Running Azure Functions Locally 通过在本地运行配置转换来切换环境 - Switching environments by running config transforms locally 在本地运行网站不会更新图像 - running website locally wont updates images 通过单元测试实例化类时,实例化类中的图像会触发System.IO异常 - When class is instantiated by Unit Test, System.IO exception is triggered for image in instantiated class C#对象即使实例化也指向null? - C# Objects pointing to null even when instantiated? 通过MSI安装时,Visual Studio扩展MEF类未实例化 - Visual Studio Extension MEF class not instantiated when installed via an MSI 如何修复错误“ Microsoft.Analytics.LocalRun.dll”,因为该错误正在由另一个进程使用。 在Visual Studio中本地运行u-sql时 - How to fix error 'Microsoft.Analytics.LocalRun.dll' because it is being used by another process. When running u-sql locally in Visual Studio 如何判断代码是否从Visual Studio / Cassini本地运行 - How to tell if code is running locally from Visual Studio/Cassini C#预处理器在本地工作,但在部署时不工作 - C# Preprocessor working locally but not when deployed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM