简体   繁体   English

log4net配置-追加程序中的GlobalContext.Property无法正常工作

[英]log4net configuration - GlobalContext.Property in appender not working

Configuring log4net in the same way I always do. 以与我以往相同的方式配置log4net。 I've set a global property called svr at Application_Start (ASP.NET Web Forms) and have set up the appender but it isn't working. 我已经在Application_Start(ASP.NET Web Forms)上设置了一个名为svr的全局属性,并设置了附加程序,但是它不起作用。

The appender is as follows: 附加器如下:

    <appender name="RollingFileAppender_Inf" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="C:\Logs\icomply_%property{svr}_info.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="-1" />
  <maximumFileSize value="50GB" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%date [%thread] %-5p %message%n" />
  </layout>
  <threshold value="INFO"/>
</appender>

In my Global.asax.cs I have 在我的Global.asax.cs中,

private void Application_Start(object sender, EventArgs e)
{
    GlobalContext.Properties["svr"] = MyEnvironment.Server;
}

but the filename is icomply_(null)_info.log and the log4net trace says this (among lots of other things) 但是文件名是icomply_(null)_info.log,log4net跟踪说明了这一点(还有很多其他事情)

    log4net: Loading Appender [RollingFileAppender_Inf] type: [log4net.Appender.RollingFileAppender]
    log4net: Parameter [file] specified subtype [log4net.Util.PatternString]
    log4net: Converter [literal] Option [C:\Logs\icomply_] Format [min=-1,max=2147483647,leftAlign=False]
    log4net: Converter [property] Option [svr] Format [min=-1,max=2147483647,leftAlign=False]
    log4net: Converter [literal] Option [_info.log] Format [min=-1,max=2147483647,leftAlign=False]
    log4net: Performing additional conversion of value from [PatternString] to [String]
    log4net: Setting Property [File] to String value [C:\Logs\icomply_(null)_info.log]

I can't see what I've done wrong - this is more or less what I always do, although this is probably the first time I've configured it in ASP.NET Web Forms. 我看不到我做错了什么—尽管这可能是我第一次在ASP.NET Web窗体中配置它,但这或多或少是我一直做的事情。

Any ideas? 有任何想法吗?

I had to add 我必须添加

log4net.Config.XmlConfigurator.Configure();

to get this working after the 使这个工作后

GlobalContext.Properties["svr"] = MyEnvironment.Server;

line. 线。

Thanks NK ;-) 谢谢NK ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM