开发者

Dynamic name in log4net

I am trying to configure a dynamic name for my log but i can't:

Here is the code in my web.config file:

<appender name="RollingFile" type="log4net.Appender.RollingPatternFileAppender">
  <file type="log4net.Util.PatternString" value="..l\log\%property{LogName}" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="-1" />
  <maximumFileSize value="5000KB" />
  <staticLogFileName value="true" />
  <countDirection value="1"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%m%n" />
  </layout>
  <filter type="log4net.Filter.PropertyFilter">
    <Key value="Version" />
    <StringToMatch value="1" />

  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />

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

</appender>
<logger name="File">
  <level value="All" />
  <appender-ref ref="RollingFile" />
</logger>

Here is the global.asax:

log4net.Config.XmlConfigurator.Configure();

The codebehind:

log = LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log4net.ThreadContext.Properties["LogName"] = 
            sito + "_Truck_Log_" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";

This does not create any log files? Why would this be?

Update 1:

I have moved log4net.Config.XmlConfigurator.Configure(); after the set thread context property but still at not work anyway.

Update 2:

@sgmoore: I am sure that I don't have permissions problems and I am sure that the "sito" variable does not contain invalid characters. I am doing something like this:

In my codebehind:

log4net.GlobalContext.Properties["LogName"] = 
           sito + "_Truck_Log_" + DateTime.Today.ToString("dd-MM-yyyy")+".txt";
log = LogManager.GetLogger(
           System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

In global.asax:

void开发者_开发知识库 Application_Start(object sender, EventArgs e) 
{
     log4net.Config.XmlConfigurator.ConfigureAndWatch(
          new System.IO.FileInfo(
            AppDomain.CurrentDomain.SetupInformation.ApplicationBase 
                                                       + "log4net.config")); 
}

But this still won't work.


You have to set the thread context property before you configure log4net.


Since you say that it does not create any file, are you sure this is not a permission issue? (Ie, does it create a file in the correct place if you don't try to use a dynamic name?) Also, are you sure the sito variable does not contain invalid characters?

I would try setting the property as earlier as possible as it may be attempting to evaluate the LogName property before it is set. (Ie set it before the GetLogger, Configure or any actual logging).

(I do something similar, but in a Winforms application and I use log4net.GlobalContext.Properties rather than log4net.ThreadContext.Properties, both of which obviously make things easier).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜