Can Spring.Net log what it's doing as it constructors objects?

旧时模样 提交于 2019-12-11 10:29:34

问题


Is there anyway to get Spring.Net to log what it's doing as it constructs objects? Something on the order of

Constructing Object A
Constructing Object B
etc etc.....

I need to resolve a circular dependency within my application, and seeing the order in which Spring is creating the objects could be a huge help.


回答1:


This can easily be done. Spring uses Common.Logging. You can grab logging output from Spring.Objects.Factory.* classes and look for ... Creating instance of Object 'your-id-here' ... messages.

Note that you have to log at DEBUG level, which means you'll see quite a lot of other information too.

The following app.config would log creation calls to the console, using log4net:

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

 <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>

<!-- this logger will catch creation messages -->

    <logger name="Spring.Objects.Factory">
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
    </logger>
  </log4net>

</configuration>

So most of this is boiler-plate Common.Logging and log4net configuration, which is well documented on the Common.Logging website. If you want to append to a file or something else, see the log4net docs.



来源:https://stackoverflow.com/questions/7475888/can-spring-net-log-what-its-doing-as-it-constructors-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!