指定记录器名称但不通过反射时,Log4net是否有效?这是一个问题吗?

时间:2013-03-26 16:49:29

标签: vb.net logging log4net console-application

我正在尝试使用log4net对一个应用程序(用VB.NET编写的控制台应用程序)日志系统进行大修。
我根据此CodeProject tutorial配置了log4net。配置完成后,我发现以下日志初始化会创建空日志(生成的文件夹结构正确,它会创建正确的日期文本文件,但文件为空):

Private ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(Reflection.MethodBase.GetCurrentMethod().DeclaringType)

但是,如果我使用以下行,它会正确记录。

Private ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger("VSED")

在我的主模块中,我正在记录错误:

log.Error("Test error!")

我正在加载大会:

<Assembly: log4net.Config.XmlConfigurator(Watch:=True)> 

以下是我的app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <file value="logs\" />
      <datePattern value="dd.MM.yyyy'.log'" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>
    <logger name="VSED">
      <level value="DEBUG"/>
      <appender-ref ref="RollingFileAppender"/>
    </logger>
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="INFO"/>
      <levelMax value="FATAL"/>  
    </filter>
  </log4net>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
</configuration>

这甚至是个问题吗?如果我在多个类中使用此记录器(显式声明其名称)而不是反射,是否会出现问题?

基本上,我只想知道为什么它不能通过首选方法工作。我的app.config不正确吗?我做错了什么吗?

谢谢大家!

1 个答案:

答案 0 :(得分:1)

您只记录来自VSED的消息,您的Reflection.MethodBase.GetCurrentMethod().DeclaringType可能不是VSED,而是something.VSED。这就是你的记录器没有提供任何输出的原因。查看Reflection.MethodBase.GetCurrentMethod().DeclaringType是什么,并使用该名称制作记录器。

这将提供所有记录器的输出:

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