vb.net 如何配置 log4net 以使 log.IsDebugEnabled 为真?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/220021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:51:42  来源:igfitidea点击:

How do I configure log4net so that log.IsDebugEnabled is true?

vb.netdebuggingconfigurationlog4net

提问by Ryan Taylor

I am trying to use log4net in an ASP.NET application with Visual Studio 2005. I have declared an instance of the logger like so:

我试图在带有 Visual Studio 2005 的 ASP.NET 应用程序中使用 log4net。我已经声明了一个记录器的实例,如下所示:

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

I am trying to use it in the following manner:

我试图以下列方式使用它:

If log.IsDebugEnabled Then
   log.Debug("Integration Services Constructed")
End If

Here is my configuration:

这是我的配置:

<log4net>

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

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="..\logs\logfile.log"/>
        <appendToFile value="true"/>
        <rollingStyle value="Size"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="1MB"/>
        <staticLogFileName value="true"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="FATAL" />
        </filter>
    </appender>

</log4net>

Unfortunately, log.IsDebugEnabledis always false.
How do I configure log4net so that I can log only debug messages?

可惜,log.IsDebugEnabled永远是假的。
如何配置 log4net 以便仅记录调试消息?

回答by Anson Smith

Before calling LogManager.GetLogger("")

在调用 LogManager.GetLogger("") 之前

You have to call log4net.Config.XmlConfigurator.Configure(); In an ASP.NET app you probably want to put this call in Application_Start

你必须调用 log4net.Config.XmlConfigurator.Configure(); 在 ASP.NET 应用程序中,您可能希望将此调用放入 Application_Start

回答by Tim Scott

Yes, do it like Anson said. Also, if you are calling Configure in a class library you can do that by adding an attribute to your class:

是的,按照安森说的去做。此外,如果您在类库中调用 Configure ,则可以通过向类添加属性来实现:

[assembly: XmlConfigurator(Watch = true)]

and if you're using log4net.configfile, use it like that instead:

如果您正在使用log4net.config文件,请改为使用它:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

回答by Anantha

If you are using a separate configuration file for log4net, do this: after following all the other setup instructions, make sure that u right click on the file in the visual studio solution explorer, select properties, expand the "Advanced" option group, set the "Copy To Output Directory" value as "Copy always". That will do the magic... :) cheers!!

如果您为 log4net 使用单独的配置文件,请执行以下操作:按照所有其他设置说明进行操作后,确保您在 Visual Studio 解决方案资源管理器中右键单击该文件,选择属性,展开“高级”选项组,设置将“复制到输出目录”值设为“始终复制”。那会变魔术... :) 欢呼!!

回答by Protector one

If you are setting log4net up in code rather than in a config file, you can call log4net.Config.BasicConfigurator.Configurebefore GetLogger.

如果您在代码中而不是在配置文件中设置 log4net,则可以log4net.Config.BasicConfigurator.ConfigureGetLogger.

回答by developer9

VB.NET -

VB.NET -

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

回答by tHiNk_OuT_oF_bOx

Use this in any method before you use log :

在使用 log 之前以任何方法使用它:

log4net.Config.XmlConfigurator.Configure();

log4net.Config.XmlConfigurator.Configure();

In App.Config ,the settings should be :

在 App.Config 中,设置应该是:

<root>
      <level value="ALL" />
      <appender-ref ref="AppenderName" />
    </root>