【问题标题】:Logging exception in WebAPIWebAPI 中的日志记录异常
【发布时间】:2017-04-04 15:03:25
【问题描述】:

我有基于 ASP.Net WebAPI 的项目,我正在使用 Log4Net 记录异常。这是异常处理的东西。

[RoutePrefix("api/customer")]
public class CustomerController : ApiController
{
    private ILogger _logger = NullLogger.Instance;

    public HttpResponseMessage Get(int id)
    {
       try
       {
          // my stuff
       }
       catch(Exception ex)
       {
            LogException(ex);
       }
    }
}

private void LogException(Exception ex)
{
    var msgParams = new Dictionary<string, string>
    {
        { "Message", ex.Message },
        { "StackTrace", ex.StackTrace },
    };
    _logger.Info(JsonConvert.SerializeObject(msgParams));
}

DI 配置。

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithConfig("log4net.xml"));
}

我的网络配置

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
  </dependentAssembly>

  <dependentAssembly>
    <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.3.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.3.1.0" />
  </dependentAssembly>
</assemblyBinding>

Log4Net.xml

<?xml version="1.0" encoding="utf-8" ?>

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">

    <file value="App.log" />

    <appendToFile value="true" />

    <maximumFileSize value="100KB" />
    <maxSizeRollBackups value="2" />

    <encoding value="utf-8" />

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %thread %logger - %message%newline" />
    </layout>

</appender>

<root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFile" />
</root>

但是当任何异常发生且控件进入 catch 块时,既不会创建任何日志文件,也不会记录异常。

我在这里缺少什么??

非常感谢任何帮助/建议。

【问题讨论】:

  • 你没有展示你的城堡初始化和注册不同的对象——比所有的程序集绑定更相关
  • 我要冒个险了,虽然我对 Log4Net 一无所知,但我说 NullLogger 类看起来非常可疑......
  • 在你的 xml debug=true 中添加到 log4net 节点,这可能会给你一些线索。

标签: c# asp.net-web-api exception-handling log4net castle-windsor


【解决方案1】:

您的记录器是private,并且您没有通过构造函数注入此依赖项。要让 Castle 通过 Property Injection 注入它,它应该是公开的。

您可以在documentation 中查看更多信息。 温莎城堡推荐best practice

private ILogger logger = NullLogger.Instance;

public CustomerService()
{
}

public ILogger Logger
{
   get { return logger; }
   set { logger = value; }
}

【讨论】:

  • 如果这是原因,那么我应该得到 Null Exception
  • 但在我的情况下,我既没有收到任何异常,也没有记录任何异常
  • @Kgn-web - 不,你不会因为NullLogger 不为空。这是一个什么都不做的记录器。 Castle 的一个好方法是让您只需调用记录器而不用担心它是否实际设置(因为通常记录器是副业和类的弱依赖)
  • @Kgn-web - 如果你调试你的代码,你会看到记录器被调用,如果你检查你会看到它不是空的,而是一个正确的对象 - 可以检查实现,但它可能只有函数的空实现
  • 让我再检查一次。但是我在帖子中添加了我的 DI 内容。请看一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 2020-12-31
相关资源
最近更新 更多